iOS Touch事件不响应的一种情况

今天遇见了一件很诡异的情况,在真机上调试发现点击UIView的时候会出现选中状态未更新的问题,打了断点后发现逻辑又是正常的,每次点击都会进入响应事件的方法,然后初步把bug定位在UI的更新阻碍了事件的响应,在touch事件分别打了断点逐步查看:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self setBackgroundColor:[UIColor colorWithRed:0.969 green:0.969 blue:0.969 alpha:1]];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self setBackgroundColor:[UIColor whiteColor]];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"🐶🐶🐶🐶🐶🐶");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setBackgroundColor:[UIColor whiteColor]];
if (self.editable) {
    [self switchSelectMode];
}
if(_clickTargetObj && _clickTargetAction && [_clickTargetObj respondsToSelector:_clickTargetAction]){
    [self setBackgroundColor:[UIColor colorWithRed:0.969 green:0.969 blue:0.969 alpha:1]];
    [_clickTargetObj performSelector:_clickTargetAction withObject:self];
    
    __weak UIBaseFileListView *me = self;
    [UIView animateWithDuration:0.1f animations:^{
        [me setBackgroundColor:[UIColor whiteColor]];
    } completion:^(BOOL finished) {
    }];
}

结果发现在频繁点击的时候连touchBegin事件都没有响应,然后检查代码,发现在注释掉[UIView animateWith...]的动画后正常了。
感觉是UIView在执行动画的时候会屏蔽掉touch事件,默认把userInteractionEnabled改为false

更新

查了下SDK,在userInteractionEnabled的文档里有提到这个问题:

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. You can disable this behavior by specifying theUIViewAnimationOptionAllowUserInteraction
option when configuring the animation.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 58,664评论 51 604
  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 6,183评论 4 26
  • 本文来自:http://ios.jobbole.com/84081/ 前言: 按照时间顺序,事件的生命周期是这样的...
    HackerOnce阅读 2,880评论 1 10
  • 赵炎被彻底的孤立了,也就意味彻底“臭了”。大家不愿意与他扯上任何关系。 “你同事赵炎怎么回事?”有人问黄华安。 “...
    ZHANG顽石点头阅读 176评论 0 0
  • 情感书写于笔下 成了情诗 情感谱写成曲调 有了情歌 情感久驻回忆 充满沧桑 情感抛向空中 随风流浪 如果人生是用来...
    Soulhome阅读 247评论 11 1