UITapGestureRecognizer does not start subclass of UIView

I created a custom subclass of UIView because I needed to override the drawRect method. I am trying to add a UITapGestureRecognizer to it, but this does not work for me. Here is the code of my 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 the object of the UIView subclass, the gesture recognizer does not start.

Many thanks!

FIXED EXIT!

My view was on UIImageView, and that UIImageView did not set userInteractionEnabled to YES.

+6
source share
4 answers

FIXED EXIT!

My view was in UIImageView, and that UIImageView did not set userInteractionEnabled to YES.

+13
source

Make sure you display it somewhere ( addSubview ).

0
source

Also ensure that userInteractionEnabled is set to true. If so, send the code that creates and connects the gesture recognizer.

0
source

Set the UserInteractionEnable parameter to true in the view you are adding and in the selection method, pass the object along with it.

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

and make a selector method like

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

Let me know if you still have a problem. Thanks!

0
source

Source: https://habr.com/ru/post/917785/


All Articles