您的当前位置:首页正文

UIControlEventTouchDown 延时响应

来源:花图问答
  • 将按钮放在view的底部,按钮的左边边处的UIControlEventTouchDown事件延迟响应。(只在真机上有此现象)
    1516417702665.jpg
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor blueColor]];
    [button setTitle:@"normal" forState:UIControlStateNormal];
    [button setTitle:@"highlighted" forState:UIControlStateHighlighted];
    button.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 30, [[UIScreen mainScreen] bounds].size.width, 30);
    
    [button addTarget:self action:@selector(touchDownAction) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(touchUpInsideAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];

解决办法:将window上手势的delaysTouchesBegan属性设置为NO。

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
        NSLog(@"gesture = %@",gesture);
        gesture.delaysTouchesBegan = NO;
        NSLog(@"delaysTouchesBegan = %@",gesture.delaysTouchesBegan?@"YES":@"NO");
        NSLog(@"delaysTouchesEnded = %@",gesture.delaysTouchesEnded?@"YES":@"NO");
    }
}

如果含有导航栏,另加一句代码:

 self.navigationController.interactivePopGestureRecognizer.delaysTouchesBegan = NO;

最后,感谢八点钟学院的老师帮我解答问题。