我创建了一个直接从类UIView
派生的自定义控件。现在,如果用户点击视图的特定部分,我想执行一个操作。所以我已经重写了touchesBegan
,touchesEnded
和touchesCancelled
方法。问题是,如果我只点击显示,就永远不会调用touchesEnded
方法。称为touchesCancelled
的方法。仅当我执行某些手势(滑动,移动,...)时,才会调用touchesEnded
。
我是否需要配置视图以启用轻击手势?
我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesBegan");
self->touchDown = YES;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.value = 1.0;
} completion:nil];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->touchDown) {
NSLog(@"touchesEnded");
self->touchDown = NO;
[UIView animateWithDuration:0.3 animations:^{
self.value = 0.0;
} completion:nil];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->touchDown) {
NSLog(@"touchesCancelled");
self->touchDown = NO;
[UIView animateWithDuration:0.3 animations:^{
self.value = 0.5;
} completion:nil];
}
}
对于轻击手势,我得到:2018-07-17 09:55:20.994645 + 0200 iOS测试[33049:2763212] touchesBegan
2018-07-17 09:55:21.092409 + 0200 iOS测试[33049:2763212] touchesCancelled
最佳答案
您是否尝试过在视图中设置recognizer.cancelsTouchesInView = NO;
一个布尔值,影响在识别手势时是否将触摸传递给视图。
关于ios - 如何使用touchesBegan和touchesEnded检测点击手势,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51376735/