iOS呈现视图控制器动画为' Push' (左右动画)

时间:2016-02-24 19:04:17

标签: ios swift2 xcode7 custom-transition

目前,我有一个视图控制器呈现其他视图控制器。我想要做的是重新创建推动视图控制器时使用的默认动画。

我目前的做法是:

FirstViewController

@IBAction private func push(sender: AnyObject) {
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SecondViewController")

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight

    view.window?.layer.addAnimation(transition, forKey: kCATransition)

    presentViewController(vc, animated: false, completion: nil)
}

SecondViewController

@IBAction private func pop(sender: AnyObject) {

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft

    view.window?.layer.addAnimation(transition, forKey: kCATransition)

    dismissViewControllerAnimated(false, completion: nil)
}

它几乎正常工作,但我有一种奇怪的行为,我在视图控制器之间转换时有一种黑屏/闪光。我已经尝试更改window.backgroundColor,但它没有解决问题。

先谢谢0_0 ......

1 个答案:

答案 0 :(得分:1)

问题仅在于您所做的不是如何为当前/解除过渡自定义动画。 Apple为您提供了明确,完善的官方方式,而您正在做的不是它。您需要为您呈现的视图控制器提供transitioningDelegate以及animationControllerForPresentedController:animationControllerForDismissedController:的实现,并实现UIViewControllerAnimatedTransitioning协议,可能还有自定义UIPresentationController子类。