iOS UICollectionViewCell 点击事件自动化埋点
iOS 中经常要进行埋点,我们这里支持 UICollectionViewCell. 进行自动化埋点,思路:
通过hook UICollectionViewCell 的setSelected:方法,
则新的方法中执行埋点逻辑,并调用原来的方法
直接上代码
@implementation UICollectionViewCell (LB)
+ (void)load
{
instanceMethodExchangeImplementations([self class], @selector(setSelected:), [self class], @selector(setSelectedWithFilter:));
}
-(void)setSelectedWithFilter:(BOOL)selected
{
// 过滤拼音键盘提示词
if (selected && !self.skipTrack ) {
UIView *tempSuperView = self.superview;
while (tempSuperView) {
if ([tempSuperView isKindOfClass:[UICollectionView class]]) {
break;
}
tempSuperView = tempSuperView.superview;
}
// 非LBScrllView的cell,才由setSelected触发点击采集,LB cell由didSelected触发,
if (tempSuperView && [tempSuperView isKindOfClass:[UICollectionView class]]
&& ![tempSuperView isKindOfClass:[LBCollectionView class]] && ![tempSuperView isKindOfClass:[LBScrollView class]]) {
UICollectionView *collectionView = (UICollectionView *)tempSuperView;
if (![collectionView isDragging] && ![collectionView isTracking] && ![collectionView isDecelerating]) {
[self setMonitorSelected:selected];
}
}
}
[self setSelectedWithFilter:selected];
}
- (void)setMonitorSelected:(BOOL)selected
{
if (selected && !self.skipTrack) {
//执行埋点逻辑
}
}
- (void)logClickCell
{
}
@end