【发布时间】:2013-09-13 21:50:34
【问题描述】:
我有一个按钮。我想在初次触摸时更改图像。如果触摸小于 1 秒,我希望它做 X。如果触摸长于 1 秒,我希望它做 Y。
我无法弄清楚如何处理这个问题。 UIButton 已经被证明很麻烦,所以我想我可以用 UIGestureRecognizers 或 touchesBegin: 来完成它
最初的想法是有一个UITapGestureRecognizer 来检测只是快速点击执行 X,并使用 UILongTapGestureRecognizer 处理更长的按下来执行 Y。
问题是UITapGestureRecognizer 没有标记UIGestureRecognizerStateBegan,它只会发送UIGestureRecognizerStateEnd 的通知。
所以我决定尝试结合使用覆盖touchesBegin: 和touchesEnd: 方法并使用UILongPressGestureRecognizer:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// change image
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// do X
// change image to original image
}
-(IBAction)longPressDetected:(UILongPressGestureRecognizer *)recognizer {
DLog(@"fired");
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Do y
// change image to original image
}
else if (recognizer.state == UIGestureRecognizerStateCancelled) {
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
}
}
如果UILongPressGestureRecognzier 触发,它会取消初始touchesBegan:(不会触发touchesEnded: 方法)。
但我遇到了touchesBegin: 方法启动缓慢的问题。被触发的方法有 0.5 秒的延迟。让我感到困惑的是,如果我将UILongPressGestureRecognizer 与longTap.minimumPressDuration = 0 一起使用,它会立即触发。
这是在我需要的程序中。在虚拟区域中使用它,touchesBegins: 也会立即触发。
什么可能导致它在程序中滞后?
有没有不同的方法可以获得想要的效果?
【问题讨论】:
标签: ios objective-c uigesturerecognizer touchesbegan