LFH*_*FHS 0 uiviewcontroller swift swift4
我有一个 segue,它是通过一个按钮触发的,该按钮在当前上下文中以模态方式显示另一个视图控制器。我正在使用自定义类来自定义过渡,这是这样的:
class ShowAnimator: NSObject {
}
extension ShowAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard
let fromVC = transitionContext.viewController(forKey: .from),
let toVC = transitionContext.viewController(forKey: .to) else {
return
}
let containerView = transitionContext.containerView
containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
let toFrame = transitionContext.finalFrame(for: toVC)
let screenBounds = UIScreen.main.bounds
let bottomLeftCorner = CGPoint(x: screenBounds.width, y: 0)
let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)
UIView.animate(withDuration: transitionDuration(using: transitionContext),
animations: {
fromVC.view.frame = finalFrame
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主视图控制器中,我有以下相关功能:
let showAnimator = ShowAnimator()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination
destination.transitioningDelegate = self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return showAnimator
}
Run Code Online (Sandbox Code Playgroud)
请注意,这个类是一个 ViewController 以及一个UIViewControllerTransitioningDelegate
但是,每当我按下按钮来执行我的转场时,它都会正确地执行动画,但是一旦完成,它只会显示一个黑屏,而不是下一个 ViewController。
一个问题是这一行:
containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
Run Code Online (Sandbox Code Playgroud)
您正在插入fromVC视图。那是错的。它已经存在(尽管不一定在容器中)。
而且您没有插入toVC视图。那也是错误的。您需要将其放入容器中。
| 归档时间: |
|
| 查看次数: |
1658 次 |
| 最近记录: |