[英]UIViewControllerAnimatedTransitioning shows a black screen when animating to and from view
I'm trying to create a easeInEaseOut animation for my view controllers, however, when I try to animate both the 'to' and 'from' views, the 'from' view turns black until the animation is finished.我正在尝试为我的视图控制器创建一个easeInEaseOut animation,但是,当我尝试为“to”和“from”视图设置动画时,“from”视图会变黑,直到 animation 完成。 If I only animate the 'to' view, then the new view controller animates correctly but over the top of the 'from' view.
如果我只为“到”视图设置动画,那么新视图 controller 可以正确设置动画,但在“从”视图的顶部。 I would like both of these views to move together, to give the impression that the first is sliding off to make way for the second (I guess being pushed off by the second is more accurate).
我希望这两个观点一起移动,给人一种印象,即第一个正在滑落,为第二个让路(我想被第二个推开更准确)。 Can anyone tell me why this would be?
谁能告诉我为什么会这样? I've tried to playing around with numerous settings, see code below:
我尝试过多种设置,请参见下面的代码:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toVC = transitionContext.viewController(forKey: .to), let fromVC = transitionContext.viewController(forKey: .from) else {
dLog("No to or from VC")
transitionContext.completeTransition(false)
return
}
switch animationType {
case .present:
dLog("Present VC")
transitionContext.containerView.addSubview(fromVC.view)
transitionContext.containerView.addSubview(toVC.view)
presentAnimation(with: transitionContext, fromView: fromVC.view, toView: toVC.view)
case .dismiss:
dLog("Dismiss VC")
}
}
func presentAnimation(with transitionContext: UIViewControllerContextTransitioning, fromView: UIView, toView: UIView) {
fromView.clipsToBounds = true
toView.clipsToBounds = true
fromView.transform = CGAffineTransform(translationX: 0, y: 0)
toView.transform = CGAffineTransform(translationX: toView.frame.size.width, y: 0)
let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .curveEaseInOut, animations: {
toView.transform = CGAffineTransform(translationX: 0.0, y: 0.0)
fromView.transform = CGAffineTransform(translationX: -fromView.frame.size.width, y: 0)
}) { _ in
transitionContext.completeTransition(true)
}
}
If anyone else is struggling with this, I figured it out.如果其他人为此苦苦挣扎,我想通了。 You have to use animateKeyFrames instead:
您必须改用 animateKeyFrames :
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: .calculationModeLinear, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 2.0) {
toView.transform = CGAffineTransform(translationX: 0, y: 0)
}
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 2.0) {
fromView.transform = CGAffineTransform(translationX: -fromView.frame.size.width, y: 0)
}
}) { _ in
transitionContext.completeTransition(true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.