Skip to content

Commit

Permalink
Fix interactive opacity while dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Mar 28, 2022
1 parent 30c9a68 commit f7c28b0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Sources/SwiftUIPager/PagerContent+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,9 @@ extension Pager.PagerContent {
guard let displayedItem = dataDisplayed.first(where: { $0 == data[page] }) else { return 1 }
guard let displayedIndex: Int = dataDisplayed.firstIndex(of: displayedItem) else { return 1 }
let totalIncrement = abs(totalOffset / pageDistance)
let currentIndex = direction == .forward ? CGFloat(index) + totalIncrement : CGFloat(index) - totalIncrement
let currentIndex = direction == .forward ? CGFloat(index) - totalIncrement : CGFloat(index) + totalIncrement

let distance = abs(currentIndex - CGFloat(displayedIndex))
print("item: \(item.id) distance: \(distance), \(index)")
return Double(max(0, min(1, 1 - distance * CGFloat(opacityIncrement))))
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ final class PagerContent_Helper_Tests: XCTestCase {
let neighbor2 = PageWrapper(batchId: 1, keyPath: \.self, element: 3)
let neighbor3 = PageWrapper(batchId: 1, keyPath: \.self, element: 4)
XCTAssertEqual(Int((pagerContent.opacity(for: focusedItem) * 10).rounded()), 8)
XCTAssertEqual(Int((pagerContent.opacity(for: neighbor1) * 10).rounded()), 8)
XCTAssertEqual(Int((pagerContent.opacity(for: neighbor2) * 10).rounded()), 4)
XCTAssertEqual(Int((pagerContent.opacity(for: neighbor3) * 10).rounded()), 0)
XCTAssertEqual(Int((pagerContent.opacity(for: neighbor1) * 10).rounded()), 4)
XCTAssertEqual(Int((pagerContent.opacity(for: neighbor2) * 10).rounded()), 8)
XCTAssertEqual(Int((pagerContent.opacity(for: neighbor3) * 10).rounded()), 4)
}

func test_GivenMultiplePaginationPager_WhenDragResult_ThenValues() {
Expand Down

0 comments on commit f7c28b0

Please sign in to comment.