我正在尝试为简单的 UIView 制作手势识别器:
UIView *theView = [[UIView alloc] initWithFrame:rect];
[theView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap)] autorelease];
[theView addGestureRecognizer:tap];
如果我调试 View 的属性 gestureRecognizers,它会显示手势识别器对象。但是当我在 View 内部点击时它不起作用。
使用 UIImageView 的相同代码工作完美,任何想法为什么在 UIView 中不起作用?
更新:
一个示例类。
@implementation ExampleClass
- (UIView *)getViewInRect:(CGRect)rect
{
UIView *theView = [[UIView alloc] initWithRect:rect];
[theView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTap)]
autorelease];
[aText addGestureRecognizer:tap];
return theView;
}
- (UIImageView *)getImageViewInRect:(CGRect)rect
{
UIImageView *theView = [[UIImageView alloc] initWithRect:rect];
[theView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTap)]
autorelease];
[theView addGestureRecognizer:tap];
return [theView autorelease];
}
- (void)handleTap
{
NSLog(@"Tap Handled!!", nil);
}
@end
更新 2:
将 UITapGestureRecognizer 添加到 theView 的所有 subview 并不能解决问题...
修复它!!!
好的!!问题是 theView 的 CGRect,它的宽度设置为 0.0!!!
最佳答案
你试过吗?
[view setUserInteractionEnabled:YES];
关于iphone - UITapGestureRecognizer 无法添加到 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485216/