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

时间:2015-07-13 11:41:26

标签: ios swift segue swipe

此代码可以正常使用向下动画进行自定义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)

}    

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

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

1 个答案:

答案 0 :(得分:6)

动画方向

您只需更改初始帧和目标帧即可。目前secondVCView的初始帧的X,Y原点0.0screenHeight,意味着它位于屏幕底部的正下方。如果您希望它从右侧显示,则需要将其更改为screenWidth0.0

然后firstVCView的目标框架会发生变化,因此原点为-screenWidth0.0secondVCView的目标框架会发生变化,原点为{{1} },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) } } } 方法以接受showSecondViewController参数并检查其UISwipeGestureRecognizer属性。当statestate时,如果您想要在用户抬起手指时触发segue,请触发segue。

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