Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): improved accuracy of ViewPager's ScrollStateChanged API #3991

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
35 changes: 28 additions & 7 deletions ios/sdk/component/viewPager/HippyViewPager.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
#import "HippyViewPagerItem.h"
#import "HippyI18nUtils.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 @@ -181,8 +188,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 @@ -225,7 +239,8 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}
}
if (self.onPageScrollStateChanged) {
self.onPageScrollStateChanged(@{ @"pageScrollState": @"dragging" });
HippyLogTrace(@"[HippyViewPager] dragging --- (BeginDragging)");
self.onPageScrollStateChanged(@{ HippyPageScrollStateKey : HippyPageScrollStateDragging });
}
}

Expand Down Expand Up @@ -280,8 +295,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 @@ -295,7 +311,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 @@ -306,6 +323,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 @@ -454,8 +476,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
Loading