我很想为一个简单的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添加到View的所有子视图都无法解决问题......
修复它!!!
行!!问题是CGRect for theView,它的宽度设置为0.0 !!!
答案 0 :(得分:19)
你试过这个吗?
[view setUserInteractionEnabled:YES];
答案 1 :(得分:2)
在.h文件中将视图声明为ivar。合成然后调用它:
[self.theview setMultipleTouchEnabled:YES];
不要忘记在viewDidLoad方法中分配和初始化该视图。
就是这样。