我在 iOS 应用程序中使用 UIGestureRecognizer,但遇到了一些问题。
我只希望手势在视图的某个区域起作用,因此我使用特定框架创建了一个新的 UIView 并将其添加到根视图中。手势工作正常,但现在唯一的问题是我无法单击新视图下方/后面的内容(根视图上的对象)。如果我将 userInteractionEnabled 设置为 NO,它会破坏手势,因此这不是一个选项。
我能做什么来解决这个问题?
Thanks.
不要为手势识别器创建新视图。识别器实现 locationInView: 方法。为包含敏感区域的视图进行设置。在handleGesture上,对您关心的区域进行命中测试,如下所示:
0) 在包含您关心的区域的视图上执行所有这些操作。不要仅为手势识别器添加特殊视图。
1) 设置 mySensitiveRect
@property (assign, nonatomic) CGRect mySensitiveRect;
@synthesize mySensitiveRect=_mySensitiveRect;
self.mySensitiveRect = CGRectMake(0.0, 240.0, 320.0, 240.0);
2)创建你的手势识别器:
gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
// if not using ARC, you should [gr release];
// mySensitiveRect coords are in the coordinate system of self.view
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.view];
if (CGRectContainsPoint(mySensitiveRect, p)) {
NSLog(@"got a tap in the region i care about");
} else {
NSLog(@"got a tap, but not where i need it");
}
}
敏感矩形应在 myView 的坐标系中初始化,该坐标系与您附加识别器的视图相同。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)