简体   繁体   English

UITapGestureRecognizer不会启动UIView子类

[英]UITapGestureRecognizer doesn't fire up on UIView Subclass

I've created a custom subclass of UIView because I needed to override drawRect method. 我已经创建了UIView的自定义子类,因为我需要覆盖drawRect方法。 I try to add a UITapGestureRecognizer to it, but this doesn't work for me. 我尝试添加一个UITapGestureRecognizer,但这对我不起作用。 Here, the code of my ViewController: 这里,我的ViewController的代码:

MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectFrame(0, 30, 30, 30)];
[customView setUserInteractionEnabled:YES];

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
[customView addGestureRecognizer:tapGestureRecognizer];

I don't understand why when I touch my UIView subclass object, the gesture recognizer doesn't fire up. 我不明白为什么当我触摸我的UIView子类对象时,手势识别器不会启动。

Thanks a lot in advance ! 非常感谢提前!

FIXED IT OUT ! 固定了!

My view was on a UIImageView, and that UIImageView hadn't the userInteractionEnabled set to YES. 我的观点是在UIImageView上,而UIImageView没有将userInteractionEnabled设置为YES。

FIXED IT OUT ! 固定了!

My view was on a UIImageView, and that UIImageView hadn't the userInteractionEnabled set to YES. 我的观点是在UIImageView上,而UIImageView没有将userInteractionEnabled设置为YES。

确保你在某处显示它( addSubview )。

Also make sure that userInteractionEnabled is set to true. 还要确保userInteractionEnabled设置为true。 If that's true, post the code that creates and hooks up the gesture recognizer. 如果这是真的,请发布创建并挂钩手势识别器的代码。

Set the UserInteractionEnable check to true on the subview that you are adding and in the selector method, do pass the object along with it ie 在要添加的子视图上将UserInteractionEnable检查设置为true,并在选择器方法中,将对象与其一起传递,即

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)];

and make the selector method as 并将选择器方法设为

-(void) doSomething:(UITapGestureRecognizer *)gesture;

Let me know if you are still having an issue. 如果您还有问题,请告诉我。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM