我正试图为我的观点创建一个自定义手势识别器。我遵循这里提到的这个答案:但出于某种原因,触摸结束和也接触到的电影都没有被调用。只有触碰才开始被召唤。
SubClass
#import "TapGesture.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation TapGesture
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIGestureRecognizerStatePossible) {
self.state = UIGestureRecognizerStateRecognized;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
@end我正在初始化TapGesture如下:
TapGesture *tapGesture=[[TapGesture alloc]initWithTarget:self action:@selector(incrementValue)];
tapGesture.delaysTouchesEnded=NO;
tapGesture.cancelsTouchesInView=NO;
[_workButton addGestureRecognizer:tapGesture]; //_workButton is a UIView我在视野中没有其他手势识别器。如果我在UIView中使用相同的方法,所有这些方法都会按预期的方式调用。
为什么在UIGestureRecogniser类中越权时不能调用touchesEnded/touchesMoved?
发布于 2016-01-21 21:28:01
在子类化UIGestureRecognizer时,您必须让它表现得像一个连续的手势,自己处理它的状态机(即手动设置它是state)。
从iOS开发人员库文档到UIGestureRecognizer
子类必须在状态之间转换时将状态属性设置为适当的值。
有关更多信息,请看这里 (滚动到Subclasing )
state读/写而不是只读,您应该使用UIGestureRecognizerSubclass.h,这在文档中也有说明:
状态属性在UIGestureRecognizer.h中声明为只读。此属性声明用于手势识别器的客户端。UIGestureRecognizer的子类必须导入UIGestureRecognizerSubclass.h。此头文件包含使其读写的状态重声明。发布于 2018-04-05 17:56:08
我发现这是双轻拍手势所必需的,但对于一个轻拍手势则不是这样:
doubleTapGestureRecognizer.delaysTouchesEnded = NO;https://stackoverflow.com/questions/34934555
复制相似问题