我的视图控制器有两个识别器:
(viewDidLoad中)
//Add gestures
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:nil];
[tap setDelaysTouchesBegan:YES];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:nil];
leftSwipe.delegate = self;
[self.view addGestureRecognizer:leftSwipe];
点击我希望发生一些事情,刷卡我想做点别的事情。但是,点击始终同时调用滑动手势和点按手势。
如何根据动作调用它?
以下是我尝试辨别(但失败)的代码
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
UISwipeGestureRecognizer *rg = (UISwipeGestureRecognizer*)gestureRecognizer;
if (rg.direction == UISwipeGestureRecognizerDirectionRight && !self.itemsOnScreen) {
NSLog(@"swipe");
return YES;
}else{
return NO;
}
}
if (self.itemsOnScreen) {
self.itemsOnScreen = NO;
//Actions for my tap
return YES;
}else{
return NO;
}
}
答案 0 :(得分:1)
您可以为点击和滑动设置不同的方法,并为每个方法设置为操作,
你可以做点什么,
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[tap setDelaysTouchesBegan:YES];
[self.view addGestureRecognizer:tap];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
[self.view addGestureRecognizer:leftSwipe];
}
- (IBAction)tap:(id)sender
{
NSLog(@"taptap");
}
- (IBAction)swipe:(id)sender
{
NSLog(@"swipe");
}
我测试过这对我有用。
答案 1 :(得分:1)
<强>建议强>
除了其他人提到的建议外,您可能还想实施以下手势识别器代理方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
可能......
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
以下是此主题Apple Documentation的链接。
答案 2 :(得分:0)
UIGesture Rec上有一个回调方法......
// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
//
// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
这应该可以解决你的问题