Swipe Gesture Recognizer not working for me

梦想的初衷 提交于 2019-12-11 04:00:59

问题


Im trying to add Swipe Gesture Recognizer to my app so I can swipe to either left or right to get to another view controller. I drag the object to the viewcontroller that I want, link it up so when swiping right it should take me to another viewcontroller. I run the simulator but the swipe feature doesn't take me to the other view.

When creating a new project, just adding 3 viewcontroller and swipe gestures I can swipe in the simulator and it takes me to another viewcontroller. But doing the exact same thing in my current app doesn't do anything.

Any ideas? I've tried to play around with enable/disable state but still get nothing


回答1:


If you are comfortable with writing codes then please try this.

UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(PerformAction:)];
left.direction = UISwipeGestureRecognizerDirectionLeft ;
[self.view addGestureRecognizer:left];

UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(PerformAction:)];
right.direction = UISwipeGestureRecognizerDirectionRight ;
    [self.view addGestureRecognizer:right];

Now perform action like this :

-(void)PerformAction:(UISwipeGestureRecognizer *)sender {
    if(sender.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"RIGHT GESTURE");
        // Perform your code here
    }

    if(sender.direction == UISwipeGestureRecognizerDirectionLeft) {
       NSLog(@"LEFT GESTURE");
       // Perform your code here
    }    
}

If you want to perform this on each viewcontroller then you have to device an intelligent logic to perform specific tasks on your left swipe & right swipe, based on your current viewcontroller.

Hope that helps.




回答2:


sometimes when recognizers overlap (like when you have a scrollView with a swipe recognizer) it can cause the touches to be cancelled.. try making sure nothing is interrupting your recognizer.



来源:https://stackoverflow.com/questions/20596669/swipe-gesture-recognizer-not-working-for-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!