Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class PagerViewViewManager : ViewGroupManager<NestedScrollableHost>(), RNCViewPa
ViewPager2.SCROLL_STATE_SETTLING -> "settling"
else -> throw IllegalStateException("Unsupported pageScrollState")
}
// Emit a final onPageScroll with offset 0 when idle to clear
// any residual floating-point offset from the scroll animation
if (state == ViewPager2.SCROLL_STATE_IDLE) {
UIManagerHelper.getEventDispatcherForReactTag(reactContext, host.id)?.dispatchEvent(
PageScrollEvent(host.id, vp.currentItem, 0f)
)
}
UIManagerHelper.getEventDispatcherForReactTag(reactContext, host.id)?.dispatchEvent(
PageScrollStateChangedEvent(host.id, pageScrollState)
)
Expand Down
26 changes: 21 additions & 5 deletions ios/PagerScrollDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ class PagerScrollDelegate: NSObject, UIScrollViewDelegate, UICollectionViewDeleg
delegate?.onPageScroll(data: eventData)
originalDelegate?.scrollViewDidScroll?(scrollView)
}

/// Emits a final onPageScroll with offset 0 before transitioning to idle,
/// clearing any residual floating-point offset from the scroll animation.
private func emitIdleWithCleanOffset(_ scrollView: UIScrollView) {
let isHorizontal = orientation == .horizontal
let pageSize = isHorizontal ? scrollView.frame.width : scrollView.frame.height
let contentOffset = isHorizontal ? scrollView.contentOffset.x : scrollView.contentOffset.y

if pageSize > 0 {
let page = Int(round(contentOffset / pageSize))
let eventData = OnPageScrollEventData(position: Double(page), offset: 0)
delegate?.onPageScroll(data: eventData)
}

delegate?.onPageScrollStateChanged(state: .idle)
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
delegate?.onPageScrollStateChanged(state: .dragging)
Expand All @@ -45,18 +61,18 @@ class PagerScrollDelegate: NSObject, UIScrollViewDelegate, UICollectionViewDeleg
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
delegate?.onPageScrollStateChanged(state: .idle)
emitIdleWithCleanOffset(scrollView)
originalDelegate?.scrollViewDidEndDecelerating?(scrollView)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
delegate?.onPageScrollStateChanged(state: .idle)
emitIdleWithCleanOffset(scrollView)
originalDelegate?.scrollViewDidEndScrollingAnimation?(scrollView)
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
delegate?.onPageScrollStateChanged(state: .idle)
emitIdleWithCleanOffset(scrollView)
}
originalDelegate?.scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate)
}
Expand Down
Loading