ios - 滑动手势识别器对我不起作用

标签 ios objective-c uiswipegesturerecognizer

我正在尝试将 Swipe Gesture Recognizer 添加到我的应用程序中,这样我就可以向左或向右滑动以转到另一个 View Controller 。我将对象拖到我想要的 View Controller ,将其链接起来,这样当向右滑动时它应该将我带到另一个 View Controller 。我运行了模拟器,但滑动功能没有将我带到另一个 View 。

创建新项目时,只需添加 3 个 View Controller 和滑动手势,我就可以在模拟器中滑动,它会将我带到另一个 View Controller 。但是在我当前的应用程序中做完全相同的事情并没有做任何事情。

有什么想法吗?我试过启用/禁用状态,但仍然一无所获

最佳答案

如果您对编写代码感到满意,那么请试试这个。

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];

现在执行这样的操作:

-(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
    }    
}

如果您想在每个 View Controller 上执行此操作,那么您必须根据当前的 View Controller 设置智能逻辑,以在您的左滑和右滑上执行特定任务。

希望对您有所帮助。

关于ios - 滑动手势识别器对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20596669/

相关文章:

ios - 完成后如何取消设置虚线?

ios - Xcode 6 : Images Not Showing Up On Device

ios - 快速实现中的 A*路径

objective-c - @autorelease 池和循环(for、while、do)语法

ios - swift : show another view controller on swipe up in first view controller

ios - UICollectionViewController Thumbnail ImageView 全屏和滚动手势

ios - 如何在某些情况下更改以编程方式创建的 UIButton 的标题和操作

ios - 为什么 UIView 的这些自动布局约束会阻止 UIButton subview 接收触摸事件?

ios - 在 iOS 的 Charts 库中实现 ChartValueSelected 方法

ios - 如何使用 UISwipeGestureRecognizer 滑动以滑入并刷新相同的 uitableview