今天遇見了一件很詭異的情況爆捞,在真機(jī)上調(diào)試發(fā)現(xiàn)點(diǎn)擊UIView的時候會出現(xiàn)選中狀態(tài)未更新的問題秸歧,打了斷點(diǎn)后發(fā)現(xiàn)邏輯又是正常的卒稳,每次點(diǎn)擊都會進(jìn)入響應(yīng)事件的方法蟆淀,然后初步把bug定位在UI的更新阻礙了事件的響應(yīng)突琳,在touch事件分別打了斷點(diǎn)逐步查看:
- (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) {
}];
}
結(jié)果發(fā)現(xiàn)在頻繁點(diǎn)擊的時候連touchBegin
事件都沒有響應(yīng)若债,然后檢查代碼,發(fā)現(xiàn)在注釋掉[UIView animateWith...]
的動畫后正常了拆融。
感覺是UIView在執(zhí)行動畫的時候會屏蔽掉touch事件蠢琳,默認(rèn)把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.