問題:
在UITableViewCell 中點擊自定義View 本來想在touchesBegan和touchesEnd中各觸發(fā)一次繪制來模擬點擊高亮的效果薄霜,但只要是快速點擊就無法觸發(fā)高亮效果,從而探究原因。
解釋:
UIScrollView 中默認情況下對TouchesBegan進行了延遲(150ms)用于判斷是否進行滑動如果滑動就不將事件傳遞給子view特恬;由于延遲導致如果點擊過快舆逃,這時候touchesBegan 和 touchesEnd就緊接著發(fā)生。
如果是在touchesBegan和touchesEnd里面都進行了 setNeedsDisplay 操作背捌,標記需要繪制毙籽;而兩次的間隔時間極小,導致Runloop 只執(zhí)行一次繪制毡庆。換句話說同時標記了兩次只有一次標記繪制生效坑赡。
iOS 的屏幕刷新為60FPS烙如,也就是最多每秒發(fā)生60次的繪制。 每一幀間隔就是 1000 ms / 60 = 16.66 ms 毅否,當兩次的繪制的間隔少于16.6毫秒時無法完成兩次繪制只會觸發(fā)一次繪制亚铁。
探究:
打印tableview默認手勢
for(UIGestureRecognizer *gestureRecognizer in self.tableView.gestureRecognizers) {
NSLog(@"%@",gestureRecognizer);
}
如下:
2016-05-19 22:46:29.923 LabelTapDemo[64833:5081332] <UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x7f94e37a2360; state = Possible; delaysTouchesBegan = YES; view = <MyTableView 0x7f94e5033200>; target= <(action=delayed:, target=<MyTableView 0x7f94e5033200>)>>
2016-05-19 22:46:29.924 LabelTapDemo[64833:5081332] <UIScrollViewPanGestureRecognizer: 0x7f94e3452160; state = Possible; delaysTouchesEnded = NO; view = <MyTableView 0x7f94e5033200>; target= <(action=handlePan:, target=<MyTableView 0x7f94e5033200>)>>
可以看到一個UIScrollViewDelayedTouchesBeganGestureRecognizer,這個是私有的手勢螟加,在UIKit庫中可以看到徘溢,查看一下頭文件定義
https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIScrollViewDelayedTouchesBeganGestureRecognizer.h
@interface UIScrollViewDelayedTouchesBeganGestureRecognizer : UIGestureRecognizer {
struct CGPoint {
float x;
float y;
} _startSceneReferenceLocation;
UIDelayedAction *_touchDelay;
}
- (void).cxx_destruct;
- (void)_resetGestureRecognizer;
- (void)clearTimer;
- (void)dealloc;
- (void)sendDelayedTouches;
- (void)sendTouchesShouldBeginForDelayedTouches:(id)arg1;
- (void)sendTouchesShouldBeginForTouches:(id)arg1 withEvent:(id)arg2;
- (void)touchesBegan:(id)arg1 withEvent:(id)arg2;
- (void)touchesCancelled:(id)arg1 withEvent:(id)arg2;
- (void)touchesEnded:(id)arg1 withEvent:(id)arg2;
- (void)touchesMoved:(id)arg1 withEvent:(id)arg2;
@end
其中關于手勢延遲的處理UIDelayedAction 由這個類實現(是從名字猜想),再找這個UIDelayedAction 看看,在下面
https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIDelayedAction.h
@interface UIDelayedAction : NSObject {
SEL m_action;
BOOL m_canceled;
double m_delay;
NSString *m_runLoopMode;
NSDate *m_startDate;
id m_target;
NSTimer *m_timer;
id m_userInfo;
}
@property (readonly) BOOL _canceled;
@property (readonly) NSDate *_startDate;
@property (retain) id target;
@property (retain) id userInfo;
- (void).cxx_destruct;
- (BOOL)_canceled;
- (id)_startDate;
- (void)cancel;
- (void)dealloc;
- (double)delay;
- (id)init;
- (id)initWithTarget:(id)arg1 action:(SEL)arg2 userInfo:(id)arg3 delay:(double)arg4;
- (id)initWithTarget:(id)arg1 action:(SEL)arg2 userInfo:(id)arg3 delay:(double)arg4 mode:(id)arg5;
- (BOOL)scheduled;
- (void)setTarget:(id)arg1;
- (void)setUserInfo:(id)arg1;
- (id)target;
- (void)timerFired:(id)arg1;
- (void)touch;
- (void)touchWithDelay:(double)arg1;
- (void)unschedule;
- (id)userInfo;
@end
看到有個NSTimer蚓再,估計就是通過這個Timer來計時蜓陌,然后再延遲傳遞事件∈┟郏看到它的初始化方法有delay:(double)arg4 應該就是延遲多少時間了吧。
為了驗證這個猜想雌隅,想辦法hook 一下這兩個函數中的其中一個才行翻默。
@interface UIDelayedActionHook : NSObject
+ (void)hook;
- (id)hook_initWithTarget:(id)arg1 action:(SEL)arg2 userInfo:(id)arg3 delay:(double)arg4 mode:(id)arg5;
@end
@implementation UIDelayedActionHook
+ (void)hook {
Class aClass = objc_getClass("UIDelayedAction");
SEL sel = @selector(hook_initWithTarget:action:userInfo:delay:mode:);
// 為UIDelayedAction增加函數
class_addMethod(aClass, sel, class_getMethodImplementation([self class], sel), "v@:@@");
// 交換實現
exchangeMethod(aClass, @selector(initWithTarget:action:userInfo:delay:mode:), sel);
}
- (id)hook_initWithTarget:(id)arg1 action:(SEL)arg2 userInfo:(id)arg3 delay:(double)arg4 mode:(id)arg5 {
return [self hook_initWithTarget:arg1 action:arg2 userInfo:arg3 delay:arg4 mode:arg5];
}
void exchangeMethod(Class aClass, SEL oldSEL, SEL newSEL) {
Method oldMethod = class_getInstanceMethod(aClass, oldSEL);
assert(oldMethod);
Method newMethod = class_getInstanceMethod(aClass, newSEL);
assert(newMethod);
method_exchangeImplementations(oldMethod, newMethod);
}
用以上代碼在啟動應用時調用 [UIDelayedActionHook hook] ; 運行。
發(fā)現會調用兩次:
第一次調用不知道是干啥的恰起,長按手勢消失時間修械?
第二次才是我們想要的,確實是和手勢
UIScrollViewDelayedTouchesBeganGestureRecognizer 相關的检盼;延遲時間是0.149秒肯污,也就是上面提到的150毫秒,看來確實是這樣吨枉。
我在進一步測試蹦渣,我在自定義的view(次view就是放在UITableViewCell上的)上打印touchesBegan 和 touchesEnded 的間隔時間
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%s",__func__);
begin = CACurrentMediaTime();
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%s",__func__);
end = CACurrentMediaTime();
time = end - begin;
printf("time: %8.2f ms\n", time * 1000);
}
發(fā)現才間隔0.31ms,離16.7 ms 還差很遠呢貌亭,所以快速點擊時永遠都不會觸發(fā)兩次繪制柬唯。
也就是因為UIScrollViewDelayedTouchesBeganGestureRecognizer 把事件延遲(150ms)傳遞給自定義view,而這個時候手指都快要離開屏幕了圃庭,隨后馬上就觸發(fā)touchesEnded锄奢,所以導致間隔時間很短,無法完成兩次繪制剧腻。
自定義view放在普通View上時拘央,touchesBegan 和 touchesEnded的時間間隔:
解決辦法:
1.關閉UITableview的手勢延遲,(但這種做法不怎么好书在,因為這樣對滑動手勢有影響)
2.在自定義的view中延遲執(zhí)行繪制灰伟,相關的條件也要延遲獲取(比如:touchesEnded 中獲取點擊的point等)儒旬。
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
double delayInSeconds = .2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
_endPoint = [touch locationInView:self];
_touchPhase = touch.phase;
[self setNeedsDisplay];
});
}