UITapGestureRecognizer 无法添加到 UIView 中?
我正在尝试为一个简单的 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!
I'm triyng to make a gesture recognizer for a simple UIView:
UIView *theView = [[UIView alloc] initWithFrame:rect];
[theView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap)] autorelease];
[theView addGestureRecognizer:tap];
If I debug the property gestureRecognizers of the view it shows the gesture recognizer object. But when I tap inside the view it doesn't work.
The same code using an UIImageView works perfect, any ideas why doesn't work in UIView?
UPDATED:
An example class.
@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
UPDATED 2:
Adding UITapGestureRecognizer to all subviews of theView don't fix the problem...
FIX IT!!!
OK!! The problem was the CGRect for theView, it had the width set to 0.0!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你尝试过这个吗?
Did you try this?
在 .h 文件中将视图声明为 ivar。综合然后像这样调用它:
不要忘记在 viewDidLoad 方法中分配并初始化该视图。
就是这样。
Declare theview as ivar in .h file. Synthesize and then call it like this:
Don't forget to alloc and init that theview in viewDidLoad method.
That's it.