UIScrollView、UICollectionView、UI
2021-03-31 本文已影响0人
chic_wx
最近在做一个需求,统一管理列表页面的预加载和统计上报。简单点说,就是用户在滚动列表页时,当列表停止滚动时,请求当前可见的内容的详情接口,同时将当前可见内容的标题、id等信息上报埋点统计。
直接说结果,过程就不讲了
直接勾UIScrollView的滚动停止事件,
[UIScrollView _scrollViewDidEndDraggingForDelegateWithDeceleration:]
[UIScrollView _scrollViewDidEndDeceleratingForDelegate]
准备一个Category将这两个方法替换了,添加我们自己的逻辑就OK了
完整代码:
UIScrollView+JSBCLibrary.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIScrollView (JSBCLibrary)
@end
NS_ASSUME_NONNULL_END
UIScrollView+JSBCLibrary.m
#import "UIScrollView+JSBCLibrary.h"
#import <objc/runtime.h>
@implementation UIScrollView (JSBCLibrary)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector = NSSelectorFromString(@"_scrollViewDidEndDeceleratingForDelegate");
SEL swizzledSelector = @selector(jsbc_scrollViewDidEndDeceleratingForDelegate);
[self exchangeMethod:originalSelector swizzledSelector:swizzledSelector];
SEL originalSelector1 = NSSelectorFromString(@"_scrollViewDidEndDraggingForDelegateWithDeceleration:");
SEL swizzledSelector1 = @selector(jsbc_scrollViewDidEndDraggingForDelegateWithDeceleration:);
[self exchangeMethod:originalSelector1 swizzledSelector:swizzledSelector1];
});
}
+ (void)exchangeMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)jsbc_scrollViewDidEndDraggingForDelegateWithDeceleration:(BOOL)deceleration {
[self jsbc_scrollViewDidEndDraggingForDelegateWithDeceleration:deceleration];
if (!deceleration) {
[self jsbcDidEndScroll];
NSLog(@"------- 停止了手指拖动 %@",self);
}
}
- (void)jsbc_scrollViewDidEndDeceleratingForDelegate {
[self jsbc_scrollViewDidEndDeceleratingForDelegate];
[self jsbcDidEndScroll];
NSLog(@"------- 停止了惯性滚动 %@",self);
}
- (void)jsbcDidEndScroll {
if ([self isKindOfClass:UITableView.class]) {
UITableView *tableView = (UITableView *)self;
[[tableView visibleCells] enumerateObjectsUsingBlock:^(__kindof UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSIndexPath *indexPath = [tableView indexPathForCell:obj];
//通过indexPath获取当前可见区域cell坐标
//TODO:在这里写自己的逻辑
}];
}
else if ([self isKindOfClass:UICollectionView.class]) {
UICollectionView *collectionView = (UICollectionView *)self;
[[collectionView visibleCells] enumerateObjectsUsingBlock:^(__kindof UICollectionViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSIndexPath *indexPath = [collectionView indexPathForCell:obj];
//通过indexPath获取当前可见区域cell坐标
//TODO:在这里写自己的逻辑
}];
}
}
@end
备注说明
- 这里不适合hook
UIScrollView
的代理方法,因为代理是一对一的,业务层会受影响 - 如果有小伙伴在Hook
_scrollViewDidEndDraggingForDelegateWithDeceleration:
方法后,发现运行奔溃,很有可能是因为参数修饰符出现了问题,这里不能用id
而应该用BOOL
-
jsbc_scrollViewDidEndDraggingForDelegateWithDeceleration:(BOOL)deceleration
中的deceleration值为NO时,才是停止手指拖动