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