我将从以下方法实现UIGestureRecognizerDelegate
:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
我附上一个例子:
我在 XIB 上做的唯一一件事就是启用用户交互。这里.m
是UIViewController
:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_viewRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)];
[_viewRecognizer setDelegate:self];
_labelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
[_labelRecognizer setDelegate:self];
[self.view addGestureRecognizer:_viewRecognizer];
[self.label addGestureRecognizer:_labelRecognizer];
}
- (void)viewDidUnload {
[super viewDidUnload];
[_viewRecognizer release]; _viewRecognizer = nil;
[_labelRecognizer release]; _labelRecognizer = nil;
self.label = nil;
}
- (void)dealloc {
[_viewRecognizer release];
[_labelRecognizer release];
self.label = nil;
[super dealloc];
}
- (void)labelTap:(UIGestureRecognizer *)recognizer {
NSLog(@"labelTap");
}
- (void)viewTap:(UIGestureRecognizer *)recognizer {
NSLog(@"viewTap");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
NSLog(@"shouldRecognizeSimultaneouslyWithGestureRecognizer");
return YES;
}
然后当点击标签时,我得到以下日志:
shouldRecognizeSimultaneouslyWithGestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
labelTap
viewTap
当点击视图时:
viewTap