如何使这个segue动画退出到右侧或左侧? - 斯威夫特

sim*_*sd3 3 swipe ios segue swift

此代码可以正常使用向下动画进行自定义segue过渡.如何使动画看起来像是向左或向右转换?

另外 - 我正在使用UISwipeGestureRecognizer来触发segue.它工作正常,除了我做最小的滑动时发生segue.如果用户已将手指抬起或至少进行了较大的滑动,我将如何允许不发生segue.我需要使用scrollView吗?

下面是我的UISwipeGestureRecognizer的代码.

override func viewDidLoad() {

    var swipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "showSecondViewController")
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeGestureRecognizer)

}    
Run Code Online (Sandbox Code Playgroud)

下面的代码是我的动画代码

class FirstCustomSegue: UIStoryboardSegue {

override func perform() {

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, -screenHeight)
        secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, -screenHeight)

        }) { (Finished) -> Void in

                    self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                        animated: false,
                        completion: nil)
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ord 6

动画方向

您只需更改初始帧和目标帧即可.目前secondVCView的初步框架的X,Y原点0.0,screenHeight,这意味着它只是坐在屏幕的底部之下.如果您希望它从右侧出现,您需要将其更改为screenWidth,0.0.

然后目标框架firstVCView将改变,因此原点是-screenWidth,0.0并且目标框架secondVCView将改变,因此原点是0.0,0.0

override func perform() {

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0)
        secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)

        }) { (Finished) -> Void in

                    self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                        animated: false,
                        completion: nil)
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

延迟滑动

修改showSecondViewController方法以接受UISwipeGestureRecognizer参数并检查其state属性.如果你想要触发SEGUE当用户中断了他们的手指触发SEGUE当stateUIGestureRecognizerStateEnded.

如果您想要更精细的控制,例如当他们滑动一定距离时,您需要检查状态UIGestureRecognizerStateChanged并监控触摸的位置.我会把它留作读者练习,因为有很多例子可以快速搜索到谷歌:)