简体   繁体   中英

UITapGestureRecognizer not working

UITapGestureRecognizer is not working for me, I wonder if anyone can help.

Here is my view definition:

@interface MainDisplayView : UIView <UIGestureRecognizerDelegate>

In the implementation, I have this in a method which is definitely being called:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goToPrevious:)];
[tapRecognizer setDelegate:self];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];
[myView addGestureRecognizer:tapRecognizer];

as well as this method:

- (void)goToPrevious:(UITapGestureRecognizer*)recognizer {
    NSLog(@"GO TO PREVIOUS");
}

I am testing in the simulator, and clicking in "myView" - but nothing happens. Thanks a lot!

Edited the code formatting.

确定一下

yourView.userInteractionEnabled = YES;

Make sure that you view is not transparent - set a background color:

view.backgroundColor = [UIColor whiteColor];

Gesture recognizer will also not work when view's alpha is very close to 0.0.

Did you link the IB outlet with myView? Or, if you are creating view via code and have other views, make sure that myView lays on top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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