如何使用触摸检测敲击手势Began和触摸Ended



我创建了一个自定义控件,它直接从类UIView派生。现在,如果用户点击我视图的特定部分,我想执行一个操作。所以我高估了方法touchesBegantouchesEndedtouchesCancelled。问题是,如果我只是点击显示器,方法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〕触摸开始

2018-07-17 09:55:21.092409+0200 iOS测试〔33049:2763212〕触摸取消

您是否尝试在视图上设置recognizer.cancelsTouchesInView = NO;

一个布尔值,影响识别手势时是否将触摸传递到视图。

您应该了解一下。

来自苹果文档,

https://developer.apple.com/documentation/uikit/uigesturerecognizer?changes=_4&language=objc

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
// Sent to the gesture recognizer when one or more fingers touch down in the associated view.
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{
//Sent to the gesture recognizer when one or more fingers move in the associated view.
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
//Sent to the gesture recognizer when one or more fingers lift from the associated view.
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//Sent to the gesture recognizer when a system event (such as an incoming phone call) cancels a touch event.
}

相关内容

  • 没有找到相关文章

最新更新