我需要使用UISwipeGestureRecognizer
捕捉两种不同的翻转动作(例如,UISwipeGestureRecognizerDirectionRight
和UISwipeGestureRecognizerDirectionLeft
)。当我使用 addGestureRecognizer 方法添加两个不同的识别器时,只有最后添加的识别器才有效。我已经读过我需要实现 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizerDelegate
协议的方法,但没有任何作用。
任何人都可以帮助捕捉两个或更多相同手势的简单示例吗? 谢谢!
答案 0 :(得分:34)
这很简单:
首先我们应该创建实现UIGestureRecognizerDelegate
协议的类:
@interface MyGestureDelegate : NSObject <UIGestureRecognizerDelegate>
@implementation MyGestureDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
并像这样使用它:
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
[self.view addGestureRecognizer:swipeGestureLeft];
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[swipeGestureLeft release];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeGesture];
MyGestureDelegate *deleg = [[MyGestureDelegate alloc] init];
[swipeGesture setDelegate:deleg];
[swipeGesture release];
答案 1 :(得分:20)
答案 2 :(得分:2)
“方向 允许的滑动方向。
@property(nonatomic) UISwipeGestureRecognizerDirection direction
讨论
您可以通过使用按位或操作数指定多个UISwipeGestureRecognizerDirection
常量来指定多个方向。默认方向为UISwipeGestureRecognizerDirectionRight
。“
也就是说,您可以执行以下操作,而不是使用两个UISwipeGestureRecognizer:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
在你的行动方法中:
-(IBAction)handleSwipeGesture:(UISwipeGestureRecognizer*)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
//do left action
} else {
//do right action
}
}
更简单,更不容易发生冲突。
答案 3 :(得分:1)
我建议你阅读一下手势识别器用来识别手势的技巧。我想,第一个识别器试图识别手势,但意识到他没有必要对它做出反应,然后以某种方式他没有传递它。
阅读它们如何工作以了解如何使用它们非常有用。
希望这有帮助。
答案 4 :(得分:0)
感谢user294890的建议,我通过从列出的UIGestureRecognizerDelegate
方法返回yes成功了。
- (void)webViewDidFinishLoad:(UIWebView *)webView
。