如何使用 touchesBegan 和 touchesEnded 检测点击手势
How to detect a tap gesture using touchesBegan and touchesEnded
我创建了一个直接派生自 class 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 Test[33049:2763212] touchesBegan
2018-07-17 09:55:21.092409+0200 iOS Test[33049:2763212] touchesCancelled
你应该完成这个。
来自 Apple 文档,
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.
}
您是否尝试过在视图中设置 recognizer.cancelsTouchesInView = NO;
A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.
我创建了一个直接派生自 class 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 Test[33049:2763212] touchesBegan
2018-07-17 09:55:21.092409+0200 iOS Test[33049:2763212] touchesCancelled
你应该完成这个。
来自 Apple 文档,
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.
}
您是否尝试过在视图中设置 recognizer.cancelsTouchesInView = NO;
A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.