小编Víc*_* B.的帖子

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];
Run Code Online (Sandbox Code Playgroud)

如果我调试视图的属性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 …
Run Code Online (Sandbox Code Playgroud)

iphone tap objective-c

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

iphone ×1

objective-c ×1

tap ×1