5

我正在尝试为简单的 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!!!

4

2 回答 2

19

Did you try this?

[view setUserInteractionEnabled:YES];
于 2011-11-22T06:06:56.730 回答
2

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

[self.theview setMultipleTouchEnabled:YES];

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

而已。

于 2011-03-30T11:47:21.477 回答