如果我只是在Xcode 4.3.2上启动一个全新的Single View App项目,请使用以下代码:
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"pic.jpg"]];
[self.view addSubview:imageView];
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapRecognizer];
}
-(void) handleTap:(UIGestureRecognizer *) gesture {
NSLog(@"Image tapped on");
}
即使我点击图片,也永远不会调用handleTap
方法......这里的内容是不是正确完成了?
答案 0 :(得分:4)
您需要在图片视图中启用触摸(默认情况下已禁用),而不是在控制器的视图中:
imageView.userInteractionEnabled = YES;
答案 1 :(得分:0)
头文件中@interface的末尾是<UIGestureRecognizerDelegate>
吗?