如何让这个 segue 动画退出到右侧或左侧? - Swift

How to make this segue animation exit to right or left side? - Swift

此代码可以很好地使我的自定义 segue 过渡带有向下的动画。如何让动画看起来像是在向左或向右过渡?

此外 - 我正在使用 UISwipeGestureRecognizer 来触发转场。它工作正常,只是在我进行最小滑动时立即发生 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)
            }
    }
}

动画方向

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

然后 firstVCView 的目标帧将发生变化,因此原点为 -screenWidth0.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)
            }
    }
}

延迟滑动

修改您的 showSecondViewController 方法以接受 UISwipeGestureRecognizer 参数并检查它的 state 属性。如果您想在用户抬起手指时触发 segue,则在 stateUIGestureRecognizerStateEnded 时触发 segue。

如果您想要更细粒度的控制,例如当他们滑动一定距离时,您需要检查 UIGestureRecognizerStateChanged 的状态并监视触摸的位置。我将把它留作 reader 的练习,因为有很多示例可以快速 Google 搜索 :)