UITapGestureRecognizer无法添加到UIView?
问题描述:
我triyng做了一个简单的UIView手势识别:UITapGestureRecognizer无法添加到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的所有子视图不解决问题......
解决它!
OK !!问题是该CGView的CGRect,它的宽度设置为0.0!
答
在.h文件中声明视图为ivar。合成,然后像这样调用它:
[self.theview setMultipleTouchEnabled:YES];
不要忘记在viewDidLoad方法中alloc和init的view。
就是这样。
+1
不解决问题,抱歉:-( – 2011-03-30 11:59:11
答
你试过这个吗?
[view setUserInteractionEnabled:YES];
你的handleTap函数看起来像什么? 您是否在theView的Superview中使用了gestureRecognizer,可以捕捉到手势? – Seega 2011-03-30 10:44:25
handleTap是上述代码所在的同一类的一种方法。从这个独家新闻我可以没有问题[自我处理]。 – 2011-03-30 10:56:33
你是说如果你在上面的代码片段中用'UIImageview'代替'UIView',它的工作原理是什么? – freespace 2011-03-30 11:20:03