【问题标题】:Swipe Gesture Recognizer not working for me滑动手势识别器对我不起作用
【发布时间】:2013-12-15 16:16:14
【问题描述】:

我正在尝试将滑动手势识别器添加到我的应用程序中,这样我就可以向左或向右滑动以访问另一个视图控制器。我将对象拖到我想要的视图控制器上,将其链接起来,以便在向右滑动时将我带到另一个视图控制器。我运行了模拟器,但滑动功能没有将我带到另一个视图。

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

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

【问题讨论】:

标签: ios objective-c uiswipegesturerecognizer


【解决方案1】:

如果您对编写代码感到满意,请尝试一下。

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

如果您想在每个视图控制器上执行此操作,那么您必须根据您当前的视图控制器设置一个智能逻辑来执行左右滑动时的特定任务。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    有时当识别器重叠时(例如当您有一个带有滑动识别器的滚动视图时),它可能会导致触摸被取消。请尝试确保没有任何东西干扰您的识别器。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题并找到了一些可能的解决方案:

      • 也许你需要让你的视图 userInteractionEnabled 为真:
          override func viewDidLoad() {
             self.view.isUserInteractionEnabled = true
             // configure your gestures ...
          }
      
      • 也许您的一个 viewController 有一个与您的 UISwipeGestureRecognizer 重叠的 TableView,在这种情况下,您可以实现 UIGestureRecognizerDelegate,在 this answer 中找到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 2015-07-23
        • 1970-01-01
        相关资源
        最近更新 更多