iphone - UITapGestureRecognizer 无法添加到 UIView?

标签 iphone objective-c tap

我正在尝试为简单的 UIView 制作手势识别器:

UIView *theView = [[UIView alloc] initWithFrame:rect];
[theView setUserInteractionEnabled:YES];

UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                       action:@selector(handleTap)] autorelease];
[theView addGestureRecognizer:tap];

如果我调试 View 的属性 gestureRecognizers,它会显示手势识别器对象。但是当我在 View 内部点击时它不起作用。

使用 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 的所有 subview 并不能解决问题...

修复它!!!

好的!!问题是 theView 的 CGRect,它的宽度设置为 0.0!!!

最佳答案

你试过吗?

[view setUserInteractionEnabled:YES];

关于iphone - UITapGestureRecognizer 无法添加到 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485216/

相关文章:

iphone - UINavigationController 导航栏在横向模式下不会缩小

iphone - 如何在 iPhone 应用程序的键盘上添加图标,以隐藏键盘?

iphone - Windows 的 Objective C 编辑器

iPhone 应用程序因 admob 而崩溃?

ios - 按钮需要点击两次才能触发 iOS 上的点击事件

iphone - NSMutableArray : Memory management while calling methods

objective-c - 在 Objective-C 中,我有一个包含一个字母的 NSString。如何获取该字母的 ASCII 值?

objective-c - 取消按钮不会关闭 iOS 模拟器中的 ComposeMail 窗口

ios - 为什么/什么时候我必须点击两次才能触发 iOS 上的点击

ios - 如果我们点击背景中的任何地方,除了快速点击 popView 之外,如何删除 popView