简体   繁体   English

UIViewControllerAnimatedTransitioning 在进出视图动画时显示黑屏

[英]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.

相关问题 黑屏与UIViewControllerAnimatedTransitioning - Black Screen with UIViewControllerAnimatedTransitioning UIViewControllerAnimatedTransitioning:在自定义视图下,屏幕不断变黑 - UIViewControllerAnimatedTransitioning: screen keeps getting black under custom view UIViewControllerAnimatedTransitioning:旋转更改后的黑屏片段 - UIViewControllerAnimatedTransitioning: Black screen fragments after rotation change 当前视图显示黑屏,为什么? - Present view shows black screen, why? UINavigationController推送视图后显示黑屏 - UINavigationController shows black screen after pushing a view 更改视图时黑屏 - Black screen when changing view 当我在uipageviewcontroller上调用self.view.removeFromSuperview()时,出现黑屏 - when i call self.view.removeFromSuperview() on uipageviewcontroller then black screen shows up 当我在导航控制器中按ViewController时,显示黑屏-空视图 - when i push viewcontroller in navigation controller, shows black screen - empty view View Controller显示黑屏,而不是从Firebase数据库Swift iOS中获取用户 - View Controller shows black screen instead of fetching users from Firebase database Swift iOS 视频文件存在时,WebView显示黑屏,并显示警报 - WebView shows black screen when video file exists and alert shows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM