寫在前面
傳送門:
前面的系列章節(jié)可以查看上面連接,本章節(jié)主要是介紹 iOS全埋點(diǎn)序列文章(5)全埋點(diǎn)- 手勢事件
前言
項(xiàng)目開發(fā)中,一般只需要采集常見控件 (UILabel谴返、UIImageView)的輕拍和長按手勢蓬衡。 接下來减宣,基于以上內(nèi)容痒筒,我們分別介紹如何實(shí)現(xiàn)手勢識別器輕拍和長按手勢的全埋點(diǎn)悉抵。
UITapGestureRecognizer全埋點(diǎn)
初始思路
采集控件的輕拍手勢袜腥,我們可以通過Method Swizzling在 UITapGestureRecognizer
類中添加Target-Action
的方法见擦,添加一個(gè)新的 Target-Action
,并在新添加的Action
中觸發(fā)$AppClick
事件羹令,進(jìn)而實(shí)現(xiàn)控件 輕拍手勢全埋點(diǎn)鲤屡。
在UITapGestureRecognizer類中,用于添加Target-Action的方法有兩 個(gè):
- initWithTarget:action:
- addTarget:action:
因此福侈,我們需要對上述兩個(gè)方法分別進(jìn)行交換酒来。
下面我們介紹詳細(xì)的實(shí)現(xiàn)步驟。
第一步:創(chuàng)建UIGestureRecognizer的類別CountData
UITapGestureRecognizer+CountData.h 定義如下:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UITapGestureRecognizer (CountData)
@end
NS_ASSUME_NONNULL_END
第二步:UITapGestureRecognizer+CountData.m 文件中實(shí)現(xiàn)+load
類方法肪凛,并在+load
類方法中分別交換- initWithTarget:action:
和 - addTarget:action
: 方法
#import "UITapGestureRecognizer+CountData.h"
#import "NSObject+Swizzler.h"
#import "SensorsAnalyticsSDK.h"
@implementation UITapGestureRecognizer (CountData)
+(void)load {
/*交換initWithTarget:action:方法*/
[UITapGestureRecognizer sensorsdata_swizzleMethod:@selector(initWithTarget:action:) withMethod:@selector(countData_initWithTarget:action:)];
/*交換addTarget:action:方法*/
[UITapGestureRecognizer sensorsdata_swizzleMethod:@selector(addTarget:action:) withMethod:@selector(countData_addTarget:action:)];
}
- (instancetype)countData_initWithTarget:(id)target action:(SEL)action {
// 調(diào)用原始的初始化方法進(jìn)行對象初始化
[self countData_initWithTarget:target action:action];
// 調(diào)用添加Target-Action的方法堰汉,添加埋點(diǎn)的Target-Action
// 這里其實(shí)調(diào)用的是-countData_addTarget:action:的實(shí)現(xiàn)方法,因?yàn)橐呀?jīng)進(jìn)行交換
[self addTarget:target action:action];
return self;
}
- (void)countData_addTarget:(id)target action:(SEL)action {
// 調(diào)用原始的方法伟墙,添加Target-Action
[self countData_addTarget:target action:action];
//新增target-action翘鸭,用于觸發(fā)$AppClick事件
[self countData_addTarget:self action:@selector(countData_trackTapGestureAction:)];
}
-(void)countData_trackTapGestureAction:(UITapGestureRecognizer *)sender {
// 獲取手勢識別器的控件
UIView *tapView = sender.view;
// 暫時(shí)只處理UILabel和UIbutton兩種控件
BOOL isTrackClass = [tapView isKindOfClass:[UILabel class]] || [tapView isKindOfClass:[UIImageView class]];
if (!isTrackClass) {
return;
}
[[SensorsAnalyticsSDK sharedInstance]track:@"UITapgestureREcoginzer手勢" properties:nil];
}
@end
上述方法暫時(shí)只支持UIImageView和UILabel這兩種控件,對于其他控件戳葵,若有需求就乓,可自行擴(kuò)展。
UILongPressGestureRecognizer全埋點(diǎn)
方法原理和上面類似,創(chuàng)建UILongPressGestureRecognizer的分類拱烁。
我直接在UITapGestureRecognizer+CountData.h中增加生蚁,具體代碼如下:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UITapGestureRecognizer (CountData)
@end
@interface UILongPressGestureRecognizer (CountData)
@end
NS_ASSUME_NONNULL_END
分類.m中實(shí)現(xiàn)代碼如下:
@implementation UILongPressGestureRecognizer (CountData)
+(void)load {
/*交換initWithTarget:action:方法*/
[UILongPressGestureRecognizer sensorsdata_swizzleMethod:@selector(initWithTarget:action:) withMethod:@selector(countData_initWithTarget:action:)];
/*交換addTarget:action:方法*/
[UILongPressGestureRecognizer sensorsdata_swizzleMethod:@selector(addTarget:action:) withMethod:@selector(countData_addTarget:action:)];
}
- (instancetype)countData_initWithTarget:(id)target action:(SEL)action {
// 調(diào)用原始的初始化方法進(jìn)行對象初始化
[self countData_initWithTarget:target action:action];
// 調(diào)用添加Target-Action的方法,添加埋點(diǎn)的Target-Action
// 這里其實(shí)調(diào)用的是-countData_addTarget:action:的實(shí)現(xiàn)方法戏自,因?yàn)橐呀?jīng)進(jìn)行交換
[self addTarget:target action:action];
return self;
}
- (void)countData_addTarget:(id)target action:(SEL)action {
// 調(diào)用原始的方法邦投,添加Target-Action
[self countData_addTarget:target action:action];
//新增target-action,用于觸發(fā)$AppClick事件
[self countData_addTarget:self action:@selector(countData_trackLongPressGestureAction:)];
}
-(void)countData_trackLongPressGestureAction:(UILongPressGestureRecognizer *)sender {
// 手勢處于UIGestureRecognizerStateEnded狀態(tài)時(shí)擅笔,才觸發(fā)$AppClick事件
if (sender.state != UIGestureRecognizerStateEnded){
return;
}
// 獲取手勢識別器的控件
UIView *tapView = sender.view;
// 暫時(shí)只處理UILabel和UIbutton兩種控件
BOOL isTrackClass = [tapView isKindOfClass:[UILabel class]] || [tapView isKindOfClass:[UIImageView class]];
if (!isTrackClass) {
return;
}
[[SensorsAnalyticsSDK sharedInstance]track:@"UILongPressGestureRecognizer手勢" properties:nil];
}
@end
注意點(diǎn):
我們都知道志衣,對于任何一個(gè)手勢见芹,其實(shí)都有不同的狀態(tài),比如:
- UIGestureRecognizerStateBegan
- UIGestureRecognizerStateChanged
-
UIGestureRecognizerStateEnded
因此蠢涝,為了解決上面的問題玄呛,我們可以設(shè)計(jì)只在手勢處于 UIGestureRecognizerStateEnded狀態(tài)時(shí),才觸發(fā)$AppClick
事件和二。
最后
對于其他手勢事件全埋點(diǎn)徘铝,實(shí)現(xiàn)思路都是相同的。如果讀者有實(shí)際需求惯吕,可以按照UITapGestureRecognizer和UILongPressGestureRecognizer全埋點(diǎn)的實(shí)現(xiàn)方案自行擴(kuò)展惕它。