为什么以下代码不起作用?
- (void)viewDidLoad
{
UISwipeGestureRecognizer *oneFingerSwipeLeft =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeLeft:)];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];
UISwipeGestureRecognizer *oneFingerSwipeRight =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:oneFingerSwipeRight];
}
- (void)oneFingerSwipeLeft:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}
- (void)oneFingerSwipeRight:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}
请记住,我在裸视图前面有很多层(例如图像,按钮和标签)。这会有所作为吗?如何在所有这些图层之上识别手势?
谢谢!
答案 0 :(得分:8)
仔细检查并确保您要添加UIGestureRecognizer,并将UserInteractionEnabled
设置为YES
。即。
[self.imageView setUserInteractionEnabled:YES];
答案 1 :(得分:3)
你必须将子视图顶部的所有触摸传递到上一个视图..所以添加一个透明视图,然后将所有这些触摸传递给主视图,如线程中所述