From 2782302deb6ff19fee4236111eef92a0d14ba080 Mon Sep 17 00:00:00 2001 From: arvinwli Date: Tue, 31 Oct 2023 10:32:20 +0800 Subject: [PATCH] viewpager:support scrollEventThrottle iOS --- .../hippy/views/viewpager/HippyViewPager.java | 2 +- .../viewPager/NativeRenderViewPager.h | 1 + .../viewPager/NativeRenderViewPager.mm | 31 ++++++++++++++----- .../viewPager/NativeRenderViewPagerManager.mm | 1 + 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/viewpager/HippyViewPager.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/viewpager/HippyViewPager.java index c30de96c7fb..edd53230a1b 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/viewpager/HippyViewPager.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/viewpager/HippyViewPager.java @@ -69,7 +69,7 @@ public void run() { private final int[] mScrollOffsetPair = new int[2]; private int mNestedScrollOffset = 0; - protected int mScrollEventThrottle = 400; // 400ms最多回调一次 + protected int mScrollEventThrottle = 0; private void init(Context context, boolean isVertical) { diff --git a/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.h b/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.h index fd3a6a1f670..464e70e0ffa 100644 --- a/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.h +++ b/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.h @@ -37,6 +37,7 @@ typedef void (^ViewPagerItemsCountChanged)(NSUInteger count); @property (nonatomic, strong) HippyDirectEventBlock onPageScrollStateChanged; @property (nonatomic, assign) NSInteger initialPage; +@property (nonatomic, assign) double scrollEventThrottle; @property (nonatomic, assign) CGPoint targetOffset; @property (nonatomic, assign, readonly) NSUInteger pageCount; @property (nonatomic, copy) ViewPagerItemsCountChanged itemsChangedBlock; diff --git a/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.mm b/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.mm index 23c3daf3999..095c26cb448 100644 --- a/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.mm +++ b/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPager.mm @@ -45,7 +45,8 @@ @interface NativeRenderViewPager () @property (nonatomic, assign) CGFloat previousStopOffset; @property (nonatomic, assign) NSUInteger lastPageSelectedCallbackIndex; - +///用于滑动事件防抖 +@property (nonatomic, assign) double _lastScrollDispatchTime; @end @implementation NativeRenderViewPager @@ -62,6 +63,8 @@ - (instancetype)initWithFrame:(CGRect)frame { self.previousFrame = CGRectZero; self.scrollViewListener = [NSHashTable weakObjectsHashTable]; self.lastPageIndex = NSUIntegerMax; + self.scrollEventThrottle = 0.0; + self._lastScrollDispatchTime = -1; self.targetContentOffsetX = CGFLOAT_MAX; if (@available(iOS 11.0, *)) { self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; @@ -99,7 +102,7 @@ - (void)insertHippySubview:(UIView *)view atIndex:(NSInteger)atIndex { } [super insertHippySubview:view atIndex:(NSInteger)atIndex]; [self.viewPagerItems insertObject:view atIndex:atIndex]; - + if ([view isKindOfClass:[NativeRenderViewPagerItem class]]) { NativeRenderViewPagerItem *item = (NativeRenderViewPagerItem *)view; __weak NativeRenderViewPager *weakPager = self; @@ -117,7 +120,7 @@ - (void)insertHippySubview:(UIView *)view atIndex:(NSInteger)atIndex { return frame; }; } - + self.needsLayoutItems = YES; if (_itemsChangedBlock) { _itemsChangedBlock([self.viewPagerItems count]); @@ -175,14 +178,14 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat currentContentOffset = self.contentOffset.x; CGFloat offset = currentContentOffset - self.previousStopOffset; CGFloat offsetRatio = offset / CGRectGetWidth(self.bounds); - + if (offsetRatio > 1) { offsetRatio -= floor(offsetRatio); } if (offsetRatio < -1) { offsetRatio -= ceil(offsetRatio); } - + NSUInteger currentPageIndex = [self currentPageIndex]; NSInteger nextPageIndex = ceil(offsetRatio) == offsetRatio ? currentPageIndex : currentPageIndex + ceil(offsetRatio); if (nextPageIndex < 0) { @@ -191,14 +194,14 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (nextPageIndex >= [self.viewPagerItems count]) { nextPageIndex = [self.viewPagerItems count] - 1; } - + if (self.onPageScroll) { self.onPageScroll(@{ @"position": @(nextPageIndex), @"offset": @(offsetRatio), }); } - + for (NSObject *scrollViewListener in _scrollViewListener) { if ([scrollViewListener respondsToSelector:@selector(scrollViewDidScroll:)]) { [scrollViewListener scrollViewDidScroll:scrollView]; @@ -258,6 +261,8 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (self.onPageScrollStateChanged) { self.onPageScrollStateChanged(@{ @"pageScrollState": @"idle" }); } + //停止滚动后重置时间 + self._lastScrollDispatchTime = -1; self.isScrolling = NO; for (NSObject *scrollViewListener in _scrollViewListener) { if ([scrollViewListener respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) { @@ -490,4 +495,16 @@ - (void)autoPageDown { } } +- (bool)checkSendOnScrollEvent { + if (!self.scrollEnabled) { + return false; + } + NSTimeInterval now = CACurrentMediaTime(); + if ((self.scrollEventThrottle > 0 && self.scrollEventThrottle < (now - self._lastScrollDispatchTime) * 1000)) { + self._lastScrollDispatchTime = now; + return true; + } + return false; +} + @end diff --git a/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPagerManager.mm b/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPagerManager.mm index 95a18448dbc..fd650d714e9 100644 --- a/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPagerManager.mm +++ b/renderer/native/ios/renderer/component/viewPager/NativeRenderViewPagerManager.mm @@ -35,6 +35,7 @@ - (UIView *)view { HIPPY_EXPORT_VIEW_PROPERTY(bounces, BOOL) HIPPY_EXPORT_VIEW_PROPERTY(initialPage, NSInteger) HIPPY_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL) +HIPPY_EXPORT_VIEW_PROPERTY(scrollEventThrottle, double) HIPPY_EXPORT_VIEW_PROPERTY(onPageSelected, HippyDirectEventBlock) HIPPY_EXPORT_VIEW_PROPERTY(onPageScroll, HippyDirectEventBlock)