今天遇见了一件很诡异的情况,在真机上调试发现点击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.