【发布时间】:2018-08-02 21:24:23
【问题描述】:
我只是想切换到第二个视图控制器。我的 HomeViewController 有这个扩展:
extension HomeViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController)
-> UIViewControllerAnimatedTransitioning? {
return transition
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
}
类中定义的转换
let transition = PopAnimator()
我有一个按钮,当它被点击时应该切换视图控制器并使用自定义转换(PopAnimator):
@objc func handleTap() {
let viewController = ViewController()
viewController.transitioningDelegate = self
self.present(viewController, animated: false, completion: nil)
}
PopAnimator 类(现在真的只是一个淡入淡出):
class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let duration = 1.0
var presenting = true
var originFrame = CGRect.zero
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toView = transitionContext.view(forKey: .to)!
containerView.addSubview(toView)
toView.alpha = 0.0
UIView.animate(withDuration: duration,
animations: {
toView.alpha = 1.0
},
completion: { _ in
transitionContext.completeTransition(true)
}
)
}
}
当我单击按钮时,它会切换 viewControllers 但不使用我的自定义过渡。如果我在我的 animationController(forPresented:presenting:source:) 函数或我的 PopAnimator 类中设置断点,则无法到达。
【问题讨论】:
标签: swift animation transition transitions uiviewanimationtransition