作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从我当前的 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/
我是一名优秀的程序员,十分优秀!