我想快速向右和向左滑动

时间:2015-09-30 00:13:09

标签: ios swift storyboard viewcontroller

我在xcode 7(swift)中有一个项目,我想在故事板中加载不同的viewscontrollers设计,并且功能向左和向左滑动,然后转到下一个viewcontroller或返回。 现在我有这个,但只是从右到左淡化,我想要两个淡入淡出。

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

3 个答案:

答案 0 :(得分:8)

您还可以通过创建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
    }
}

答案 1 :(得分:2)

你说这一切都错了。您需要研究交互式自定义转换。因此,例如,如果您想左右滑动以表示导航控制器中的推送和弹出,请实现导航控制器的委托navigationController:animationControllerForOperation:fromViewController:toViewController:navigationController:interactionControllerForAnimationController:并从那里开始。

答案 2 :(得分:0)

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

对于Storyboard设置: enter image description here