我正在尝试为简单的 UIView 制作手势识别器:
UIView *theView = [[UIView alloc] initWithFrame:rect];
[theView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap)] autorelease];
[theView addGestureRecognizer:tap];
如果我调试视图的属性gestureRecognizers,它会显示手势识别器对象。但是当我在视图内部点击时它不起作用。
使用 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 的所有子视图并不能解决问题...
修理它!!!
好的!!问题是 theView 的 CGRect,它的宽度设置为 0.0!!!