Skip to content

Commit

Permalink
fix(ios): improved accuracy of ViewPager's ScrollStateChanged API
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Sep 13, 2024
1 parent c2927bb commit b0c2543
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions renderer/native/ios/renderer/component/viewPager/HippyViewPager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
#import "HippyRenderUtils.h"



static NSString *const HippyPageScrollStateKey = @"pageScrollState";
static NSString *const HippyPageScrollStateIdle = @"idle";
static NSString *const HippyPageScrollStateSettling = @"settling";
static NSString *const HippyPageScrollStateDragging = @"dragging";


@interface HippyViewPager ()
@property (nonatomic, strong) NSMutableArray<UIView *> *viewPagerItems;
@property (nonatomic, assign) BOOL isScrolling;
Expand Down Expand Up @@ -189,8 +196,15 @@ - (void)setPage:(NSInteger)pageNumber animated:(BOOL)animated {
self.targetContentOffsetX = CGRectGetMinX(theItem.frame);
[self setContentOffset:theItem.frame.origin animated:animated];
[self invokePageSelected:pageNumber];

if (self.onPageScrollStateChanged) {
self.onPageScrollStateChanged(@{ @"pageScrollState": @"idle" });
if (animated) {
HippyLogTrace(@"[HippyViewPager] settling --- (setPage withAnimation)");
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey: HippyPageScrollStateSettling });
} else {
HippyLogTrace(@"[HippyViewPager] idle ~~~~~~ (setPage withoutAnimation)");
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey: HippyPageScrollStateIdle });
}
}
}

Expand Down Expand Up @@ -233,7 +247,8 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}
}
if (self.onPageScrollStateChanged) {
self.onPageScrollStateChanged(@{ @"pageScrollState": @"dragging" });
HippyLogTrace(@"[HippyViewPager] dragging --- (BeginDragging)");
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey : HippyPageScrollStateDragging });
}
}

Expand Down Expand Up @@ -288,8 +303,9 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
self.isScrolling = NO;
}
if (self.onPageScrollStateChanged) {
NSString *state = decelerate ? @"settling" : @"idle";
self.onPageScrollStateChanged(@{ @"pageScrollState": state });
NSString *state = decelerate ? HippyPageScrollStateSettling : HippyPageScrollStateIdle;
HippyLogTrace(@"[HippyViewPager] %@ ??? (EndDragging)", state);
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey : state });
}
}

Expand All @@ -303,7 +319,8 @@ - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (self.onPageScrollStateChanged) {
self.onPageScrollStateChanged(@{ @"pageScrollState": @"idle" });
HippyLogTrace(@"[HippyViewPager] idle ~~~~~~ (EndDecelerating)");
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey : HippyPageScrollStateIdle });
}
self.isScrolling = NO;
for (NSObject<UIScrollViewDelegate> *scrollViewListener in _scrollViewListener) {
Expand All @@ -314,6 +331,11 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
if (self.onPageScrollStateChanged) {
HippyLogTrace(@"[HippyViewPager] idle ~~~~~~ (DidEndScrollingAnimation)");
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey : HippyPageScrollStateIdle });
}

for (NSObject<UIScrollViewDelegate> *scrollViewListener in _scrollViewListener) {
if ([scrollViewListener respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)]) {
[scrollViewListener scrollViewDidEndScrollingAnimation:scrollView];
Expand Down Expand Up @@ -482,8 +504,7 @@ - (void)layoutSubviews {
[self setPage:self.initialPage animated:NO];
_didFirstTimeLayout = YES;
self.needsResetPageIndex= NO;
}
else {
} else {
if (self.needsResetPageIndex) {
[self setPage:_lastPageIndex animated:NO];
self.needsResetPageIndex= NO;
Expand Down

0 comments on commit b0c2543

Please sign in to comment.