如何使这个转场动画向右或向左退出?- Swift

3
这段代码可以成功地实现自定义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个回答

6

动画方向

您只需要更改初始帧和目标帧。当前的secondVCView的初始帧的X,Y坐标为0.0, screenHeight,表示它位于屏幕底部以下。如果您想要从右侧出现,请将其更改为screenWidth0.0

然后,firstVCView的目标帧将更改,使其原点为-screenWidth0.0,而secondVCView的目标帧将更改,使其原点为0.00.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的状态并监控触摸的位置。由于有许多示例可以通过快速搜索Google获得,因此我将其留给读者作为练习 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接