Ios UISweepGestureRecognizer与UITapGestureRecognizer一起使用时未注册刷卡
我的视图控制器有两个识别器: (viewDidLoad) 轻拍时我想做点什么,轻拍时我想做点别的。但是,轻触总是同时调用滑动手势和轻触手势 我怎样才能使它根据动作调用 下面是我试图识别(但失败)的代码Ios UISweepGestureRecognizer与UITapGestureRecognizer一起使用时未注册刷卡,ios,objective-c,Ios,Objective C,我的视图控制器有两个识别器: (viewDidLoad) 轻拍时我想做点什么,轻拍时我想做点别的。但是,轻触总是同时调用滑动手势和轻触手势 我怎样才能使它根据动作调用 下面是我试图识别(但失败)的代码 您可以为轻触和滑动设置不同的方法,并将每种方法设置为操作 你可以这样做 -(void)viewDidLoad { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:
您可以为轻触和滑动设置不同的方法,并将每种方法设置为操作 你可以这样做
-(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");
}
我已经测试过这对我有用。建议 除了其他人提出的建议外,您可能还希望实现以下手势识别器委托方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
而且可能
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
这是有关此主题的链接。UIGestureRec上有一个calback方法
// 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;
这会解决你的问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
// 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;