在iOS上,为什么以下UITapgestureRecognitor(Tap)不起作用?
如果我刚刚在Xcode 4.3.2上启动一个全新的单视图应用程序项目,并使用以下代码:在iOS上,为什么以下UITapgestureRecognitor(Tap)不起作用?,ios,cocoa-touch,uiview,uiviewcontroller,Ios,Cocoa Touch,Uiview,Uiviewcontroller,如果我刚刚在Xcode 4.3.2上启动一个全新的单视图应用程序项目,并使用以下代码: @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.jpg"]
@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
方法也不会被调用。。。这里是否有一些操作不正确?是否在头文件的@interface结尾处
?是否在头文件的@interface结尾处
?您需要在图像视图中启用触摸功能(默认情况下是禁用的),而不是在控制器视图中:
imageView.userInteractionEnabled = YES;
您需要在图像视图中启用触摸(默认情况下禁用),而不是在控制器视图中:
imageView.userInteractionEnabled = YES;
默认情况下,userInteractionEnabled对于父级为true,但对于child@Vladimir完美答案!你做iOS/Cocoa编程很久了吗@safecase,对于UIView类为true,但对于某些子类为false,例如UILabel、UIImageView-您需要明确地将其设置为YES,因为默认userInteractionEnabled对于父类为true,但对于child@Vladimir完美答案!你做iOS/Cocoa编程很久了吗@safecase,对于UIView类是正确的,但对于某些子类是错误的,例如UILabel、UIImageView-您需要明确地将其设置为YES