gpt4 book ai didi

ios - 如何在将 UIViewController 呈现给自定义时更改默认转换

转载 作者:行者123 更新时间:2023-11-28 11:19:47 26 4
gpt4 key购买 nike

我正在尝试从我当前的 View Controller 中呈现另一个 UIViewController。过渡动画设置为默认值,呈现的 UIViewController 似乎是从底部到顶部出现的。但是我正在尝试将动画设置为从上到下。我怎样才能做到这一点?我尝试过使用过渡效果作为垂直覆盖、水平翻转、交叉溶解,但似乎没有一个符合我的选择。有什么建议可以实现吗?

 let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let editCategoryVC: EditCategoryVC = storyboard.instantiateViewControllerWithIdentifier("editCategoryVC")as EditCategoryVC
self.presentViewController(editCategoryVC, animated:true, completion: nil

)

最佳答案

处理动画的 TransitionHandler 辅助类

import UIKit

class TransitionHandler : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {

return 0.9
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

let container = transitionContext.containerView
let fromView = transitionContext.view(forKey: .from)!
let toView = transitionContext.view(forKey: .to)!

// e.g to show the animation from x axis change this frame as required
// let offScreenUp = CGAffineTransformMakeTranslation(-container.frame.size.width, 0)
// let offScreenDown = CGAffineTransformMakeTranslation(container.frame.size.width,0)
let offScreenUp = CGAffineTransform(translationX: 0, y: 0) // (-container.frame.size.width,0)
let offScreenDown = CGAffineTransform(translationX: 0, y: container.frame.size.height)
toView.transform = offScreenUp
container.addSubview(toView)
container.addSubview(fromView)
let duration = self.transitionDuration(using: transitionContext)

UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.8, options: [], animations: {

fromView.transform = offScreenDown
toView.transform = CGAffineTransform.identity

}, completion: { finished in

transitionContext.completeTransition(true)
})

}
}

您的类在单击按钮时显示下一个 Controller

class ViewController: UIViewController {

let transitionHandler = TransitionHandler()

@IBAction func showVCAnimation(sender: AnyObject) {

var storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextVC = storyboard.instantiateViewControllerWithIdentifier("editCategoryVC") as EditCategoryVC
nextVC.transitioningDelegate = self.transitionHandler
self.presentViewController(nextVC, animated: true, completion: nil)
}
}

好吧,感谢 AppCoda!!!

关于ios - 如何在将 UIViewController 呈现给自定义时更改默认转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29251758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com