我正在尝试为一个简单的应用程序创建一个按钮,该按钮可以响应单击,双击(即轻击,而不是两个手指点击)和长按(即点击并按住)。我已经查看了有关视图上多个触摸的选项,但这是一个按钮,即使更改了按钮提供的代码,该解决方案也不起作用。我尝试了很多不同的策略,但是我的努力所关注的两个主要策略如下:
- (void)viewDidLoad {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
UITapGestureRecognizer *twoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
longPress.minimumPressDuration = 1.0;
oneTap.numberOfTouchesRequired = 1;
twoTap.numberOfTouchesRequired = 2;
[oneTap requireGestureRecognizerToFail:twoTap];
[twoTap requireGestureRecognizerToFail:longPress];
[self.recordButton addGestureRecognizer:longPress];
[self.recordButton addGestureRecognizer:oneTap];
[self.recordButton addGestureRecognizer:twoTap];
}
在这个例子中,self.recordButton只是指按钮的名称。头文件中的这个UIButton实例链接到按钮,因此这不是问题。另外,这三个动作(即@selector(longPress :))在后面的实现中定义。
另一个是:
-(IBAction)record:(id)sender {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
UITapGestureRecognizer *twoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
longPress.minimumPressDuration = 1.0;
oneTap.numberOfTouchesRequired = 1;
twoTap.numberOfTouchesRequired = 2;
[oneTap requireGestureRecognizerToFail:twoTap];
[twoTap requireGestureRecognizerToFail:longPress];
[self.recordButton addGestureRecognizer:longPress];
[self.recordButton addGestureRecognizer:oneTap];
[self.recordButton addGestureRecognizer:twoTap];
}
记录链接到我试图获得响应的按钮。
最后两件事要注意:
只需轻轻一按(长按和双击即添加功能)按钮操作有效,因此将按钮链接到方法记录
长按和双击手势仅用于segues(我认为这是正确的词)。基本上,这两个手势用于转到不同的视图
所以我的问题是,我该怎么做?我主要只需要知道在按钮上添加多个手势背后的一般理论。