UITapGestureRecognizer 无法添加到 UIView 中?

发布于 2024-10-28 04:47:23 字数 1747 浏览 0 评论 0原文

我正在尝试为一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌吟 2024-11-04 04:47:23

你尝试过这个吗?

[view setUserInteractionEnabled:YES];

Did you try this?

[view setUserInteractionEnabled:YES];
英雄似剑 2024-11-04 04:47:23

在 .h 文件中将视图声明为 ivar。综合然后像这样调用它:

[self.theview setMultipleTouchEnabled:YES];

不要忘记在 viewDidLoad 方法中分配并初始化该视图。

就是这样。

Declare theview as ivar in .h file. Synthesize and then call it like this:

[self.theview setMultipleTouchEnabled:YES];

Don't forget to alloc and init that theview in viewDidLoad method.

That's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文