iOS呈现的视图控制器动画为“推"(左右动画) [英] iOS presenting view controller animated as 'Push' (right-left animation)

查看:151
本文介绍了iOS呈现的视图控制器动画为“推"(左右动画)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个显示其他视图控制器的视图控制器.我想做的是重新创建推送视图控制器时使用的默认动画.

Currently, I have a view controller presenting other view controller. What I'm trying to do is to recreate the default animation used when pushing a view controller.

我目前的做法是:

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,但不能解决问题.

It is almost working, but I have a weird behaviour, I'm having a kind of black screen/flash when transitioning between view controllers. I already tried changing window.backgroundColor but it is not fixing the issue.

预先感谢0_0 ...

Thanks in advance 0_0...

推荐答案

问题仅在于您在执行的操作不是如何针对当前/关闭过渡自定义动画. Apple为您提供了一种清晰,完善,正式的方法来执行此操作,而您正在执行的操作并非如此.您需要给呈现的视图控制器一个transitioningDelegate以及animationControllerForPresentedController:animationControllerForDismissedController:的实现,并实现UIViewControllerAnimatedTransitioning协议,可能还需要一个自定义的UIPresentationController子类.

The trouble is merely that what you're doing is not how to customize the animation for a present/dismiss transition. Apple has provided you with a clear, well-establish, official way to do that, and what you're doing is not it. You need to give your presented view controller a transitioningDelegate along with implementations of animationControllerForPresentedController: and animationControllerForDismissedController:, and implement the UIViewControllerAnimatedTransitioning protocol, possibly along with a custom UIPresentationController subclass.

这篇关于iOS呈现的视图控制器动画为“推"(左右动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆