From f7c28b02e8ef2a9b10fd6cc2c85fc69b943985ad Mon Sep 17 00:00:00 2001 From: fermoya Date: Mon, 28 Mar 2022 14:19:06 +0100 Subject: [PATCH] Fix interactive opacity while dragging --- Sources/SwiftUIPager/PagerContent+Helper.swift | 3 +-- Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftUIPager/PagerContent+Helper.swift b/Sources/SwiftUIPager/PagerContent+Helper.swift index a3d9517..981d8d1 100644 --- a/Sources/SwiftUIPager/PagerContent+Helper.swift +++ b/Sources/SwiftUIPager/PagerContent+Helper.swift @@ -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)))) } diff --git a/Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift b/Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift index ccdada2..153cfcb 100644 --- a/Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift +++ b/Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift @@ -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() {