Skip to content

Commit

Permalink
viewpager:support scrollEventThrottle iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinwli committed Oct 31, 2023
1 parent 5875169 commit 2782302
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ @interface NativeRenderViewPager ()

@property (nonatomic, assign) CGFloat previousStopOffset;
@property (nonatomic, assign) NSUInteger lastPageSelectedCallbackIndex;

///用于滑动事件防抖
@property (nonatomic, assign) double _lastScrollDispatchTime;
@end

@implementation NativeRenderViewPager
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -117,7 +120,7 @@ - (void)insertHippySubview:(UIView *)view atIndex:(NSInteger)atIndex {
return frame;
};
}

self.needsLayoutItems = YES;
if (_itemsChangedBlock) {
_itemsChangedBlock([self.viewPagerItems count]);
Expand Down Expand Up @@ -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) {
Expand All @@ -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<UIScrollViewDelegate> *scrollViewListener in _scrollViewListener) {
if ([scrollViewListener respondsToSelector:@selector(scrollViewDidScroll:)]) {
[scrollViewListener scrollViewDidScroll:scrollView];
Expand Down Expand Up @@ -258,6 +261,8 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (self.onPageScrollStateChanged) {
self.onPageScrollStateChanged(@{ @"pageScrollState": @"idle" });
}
//停止滚动后重置时间
self._lastScrollDispatchTime = -1;
self.isScrolling = NO;
for (NSObject<UIScrollViewDelegate> *scrollViewListener in _scrollViewListener) {
if ([scrollViewListener respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2782302

Please sign in to comment.