iOS presenting view controller animated as 'Push' (right-left animation)

◇◆丶佛笑我妖孽 提交于 2019-12-01 12:14:55

问题


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.

My current approach is:

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

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.

Thanks in advance 0_0...


回答1:


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.



来源:https://stackoverflow.com/questions/35610826/ios-presenting-view-controller-animated-as-push-right-left-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!