UITableView 手势延迟导致subview无法完成两次绘制

96
AlienJunX
2016.05.20 10:11* 字数 825

问题:
在UITableViewCell 中点击自定义View 本来想在touchesBegan和touchesEnd中各触发一次绘制来模拟点击高亮的效果,但只要是快速点击就无法触发高亮效果,从而探究原因。

解释:
UIScrollView 中默认情况下对TouchesBegan进行了延迟(150ms)用于判断是否进行滑动如果滑动就不将事件传递给子view;由于延迟导致如果点击过快,这时候touchesBegan 和 touchesEnd就紧接着发生。
如果是在touchesBegan和touchesEnd里面都进行了 setNeedsDisplay 操作,标记需要绘制;而两次的间隔时间极小,导致Runloop 只执行一次绘制。换句话说同时标记了两次只有一次标记绘制生效。

iOS 的屏幕刷新为60FPS,也就是最多每秒发生60次的绘制。 每一帧间隔就是 1000 ms / 60 = 16.66 ms ,当两次的绘制的间隔少于16.6毫秒时无法完成两次绘制只会触发一次绘制。

探究:
打印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] ; 运行。
发现会调用两次:

QQ20160519-2.png

第一次调用不知道是干啥的,长按手势消失时间?

QQ20160519-4.png

第二次才是我们想要的,确实是和手势
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);
}
QQ20160519-5.png

发现才间隔0.31ms,离16.7 ms 还差很远呢,所以快速点击时永远都不会触发两次绘制。
也就是因为UIScrollViewDelayedTouchesBeganGestureRecognizer 把事件延迟(150ms)传递给自定义view,而这个时候手指都快要离开屏幕了,随后马上就触发touchesEnded,所以导致间隔时间很短,无法完成两次绘制。

自定义view放在普通View上时,touchesBegan 和 touchesEnded的时间间隔:

8E3932E4-39D8-411D-9102-65624A01D75C.png

解决办法:
1.关闭UITableview的手势延迟,(但这种做法不怎么好,因为这样对滑动手势有影响)
2.在自定义的view中延迟执行绘制,相关的条件也要延迟获取(比如: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];
    });
}

参考资料:
http://pinka.cn/2015/06/uiwebview的scrollview和渲染机制探究/

开发
Web note ad 1