为什么不识别我的水龙头?

Tom*_*ert 2 iphone uigesturerecognizer ios

我创建了一个带有包含图像视图的笔尖的小样本项目.在我的视图控制器代码中,我为图像视图添加了一个手势识别器来检测点击.但它从不调用处理程序方法.

这是标题:

#import <UIKit/UIKit.h>

@interface TapExperimentViewController : UIViewController {
    UIImageView *imageView;
}

@property (retain) IBOutlet UIImageView *imageView;

- (void)handleTap:(UIGestureRecognizer *)sender;

@end
Run Code Online (Sandbox Code Playgroud)

这是实施文件:

#import "TapExperimentViewController.h"

@implementation TapExperimentViewController

@synthesize imageView;

- (void)dealloc {
    [imageView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(handleTap:)];
    [self.imageView addGestureRecognizer:tap];
    [tap release];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.imageView = nil;
}

- (void)handleTap:(UIGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"tap");
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

我确保插座已连接好.为什么不是handleTap:当我触摸图像时被调用?

Joe*_*Joe 5

我自己没有使用手势,但我知道UIImageView默认情况下不启用用户交互.

默认情况下,新图像视图对象配置为忽略用户事件.如果要在UIImageView的自定义子类中处理事件,则必须在初始化对象后显式将userInteractionEnabled属性的值更改为YES.