简体   繁体   中英

UISwipeGestureRecognizer not working in the iOS simulator

I have used the following code, but the when I run the program, swipe isn't working.

First I added gesture recognizer protocol in the interface section

#import <UIKit/UIKit.h>

@interface BNRViewController : UIViewController <UIGestureRecognizerDelegate>

@property (nonatomic,strong) IBOutlet UILabel *swipe;

@end

Now I instantiated swipe gesture recogniser in the implement section

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(fingerSwipe:)];

    swipeUp.numberOfTouchesRequired = 3;
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeUp];

}

can any one help me out

The gesture recognizer won't work unless you set userInteractionEnabled to YES on the label. so you must add this line [swipe setUserInteractionEnabled:YES]; it is working i have checked.

UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(fingerSwipe:)];

[swipe setUserInteractionEnabled:YES];

swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];

there might be defined more than 1 gesture for the view

you can check with this;

self.view.gestureRecognizers.count

remove all gestures and add your swipe gesture then try again

self.view.gestureRecognizers?.removeAll()
self.view.addGestureRecognizer(swipeGestureRecognizer)

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