【问题标题】:i want to swipe right and left in swift我想快速左右滑动
【发布时间】:2015-12-27 15:05:48
【问题描述】:

我在 xcode 7 ( swift ) 中有一个项目,我想加载在故事板中设计的不同视图控制器,其功能是左右滑动并转到下一个视图控制器或返回。 现在我有了这个,但只从右到左淡入,我想要两个淡入。

func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {

    switch sender.direction {
    case UISwipeGestureRecognizerDirection.Right:
        print("SWIPED DERECHA")
       self.performSegueWithIdentifier("cambio2", sender: nil)
    case UISwipeGestureRecognizerDirection.Left:
        print("SWIPED IZQUIERDA")
        self.performSegueWithIdentifier("cambio", sender: nil)
    default:
        break
    }
}

【问题讨论】:

    标签: ios swift storyboard viewcontroller


    【解决方案1】:

    您还可以通过创建 2 个 UISwipeGestureRecognizer 实例来使用它。 每个方向一个。

        var swipeLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipe:")
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    
        var swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipe:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    
        self.view.addGestureRecognizer(swipeLeft)
        self.view.addGestureRecognizer(swipeRight)
    

    还有滑动功能

    func swipe(sender: UISwipeGestureRecognizer) {
    
        switch sender.direction {
        case UISwipeGestureRecognizerDirection.Right:
            print("SWIPED DERECHA")
            self.performSegueWithIdentifier("first", sender: nil)
        case UISwipeGestureRecognizerDirection.Left:
            print("SWIPED IZQUIERDA")
            self.performSegueWithIdentifier("second", sender: nil)
        default:
            break
        }
    }
    

    【讨论】:

    • 感谢放学后的答案,我会尝试创建一个效果淡入到右和淡入到左,当我向其中一个方向滑动我认为必须尝试带有动画的导航控制器
    • 对于 Swift3:在注册以及在滑动()函数。还将“self.performSegueWithIdentifier("first", sender: nil)" 更改为 "self.performSegue(withIdentifier: "first", sender: nil)" 和 "self.performSegueWithIdentifier("second", sender: nil)" 为" self.performSegue(withIdentifier: "second", sender: nil)".
    【解决方案2】:

    你做错了。您需要研究交互式自定义转换。因此,例如,如果您想左右滑动来表示在导航控制器中推送和弹出,则实现导航控制器的代理 navigationController:animationControllerForOperation:fromViewController:toViewController:navigationController:interactionControllerForAnimationController: 并从那里开始。

    【讨论】:

      【解决方案3】:

      UISwipeGestureRecognizer 应该只允许一个方向,你应该使用UIPanGestureRecognizer

      let swipe = UIPanGestureRecognizer(target: self, action: "respondToSwipeGesture:")
              self.view.addGestureRecognizer(swipe);
      
      
      func respondToSwipeGesture(sender: UIPanGestureRecognizer) {
              let point = sender.velocityInView(self.view)
              //left or right
              if point.x > 0 {
                  self.performSegueWithIdentifier("segue1", sender: nil)
              }else{
                  self.performSegueWithIdentifier("segue2", sender: nil)
              }
      

      当然两个控制器都应该在UINavigationController

      对于情节提要设置:

      【讨论】:

      • 我成功使用了uigesture识别器,但是viewcontroller之间的转换只能在一个方向上工作我想在应用程序中创建一个典型的杂志,比如ibooks中pdf的视图
      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      相关资源
      最近更新 更多