Swift 自定义转换不起作用(未调用 UIViewControllerTransitioningDelegate)
Posted
技术标签:
【中文标题】Swift 自定义转换不起作用(未调用 UIViewControllerTransitioningDelegate)【英文标题】:Swift Custom Transition Not Working (UIViewControllerTransitioningDelegate not called) 【发布时间】: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 类中设置断点,则无法到达。
【问题讨论】:
【参考方案1】: self.present(viewController, animated: false, completion: nil)
如果您想要动画过渡,请尝试设置animated: true
【讨论】:
以上是关于Swift 自定义转换不起作用(未调用 UIViewControllerTransitioningDelegate)的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 自定义 SectionIndexTitles 在 Tableview 中不起作用
自定义 UICollectionViewCell 中的圆角在 Swift 中不起作用