问题 条件绑定的初始化程序必须具有可选类型,而不是'UIView'


通过对StackOverflow的研究,我了解到这个错误是由于尝试绑定一个不是可选的类型引起的,但是在这种情况下没有意义,因为它使用了guard。这是我的代码:

func animateTransition(_ transitionContext: UIViewControllerContextTransitioning) {
        // Here, we perform the animations necessary for the transition

        guard let fromVC = transitionContext.viewController(forKey: UITransitionContextFromViewControllerKey) else { return }
        let fromView = fromVC.view
        guard let toVC = transitionContext.viewController(forKey: UITransitionContextToViewControllerKey) else { return }
        let toView = toVC.view
        guard let containerView = transitionContext.containerView() else { return }

        if self.presenting {
            containerView.addSubview(toView)
        }

        let animatingVC = self.presenting ? toVC : fromVC
        let animatingView = animatingVC.view
        let appearedFrame = transitionContext.finalFrame(for: animatingVC)

        var alpha: CGFloat = 1
        if self.options.contains([.AlphaChange]) {
            alpha = 0;
        }

        let initialAlpha = self.presenting ? alpha : 1
        let finalAlpha = self.presenting ? 1: alpha

        var dismissedFrame = appearedFrame
        let startRect = CGRect(origin: appearedFrame.origin, size: containerView.bounds.size)
        let offset = self.calculateStartPointOffset(startRect, options: self.options)

        if options.contains([.Dissolve]) && !self.presenting {
            dismissedFrame.size = containerView.bounds.size
            dismissedFrame.origin = CGPointZero
        } else {
            dismissedFrame = CGRect(x: offset.x, y: offset.y, width: appearedFrame.width, height: appearedFrame.height)
        }

        let initialFrame = self.presenting ? dismissedFrame : appearedFrame
        let finalFrame = self.presenting ? appearedFrame : dismissedFrame
        animatingView?.frame = initialFrame
        animatingView?.alpha = initialAlpha
        let dumpingValue = CGFloat(self.options.contains([.Interactive]) ? 1 : 0.8)

        UIView.animate(withDuration: self.transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: dumpingValue, initialSpringVelocity: 0.2, options: [UIViewAnimationOptions.allowUserInteraction, UIViewAnimationOptions.beginFromCurrentState],
                                   animations:
            { () -> Void in
                animatingView?.frame = finalFrame
                animatingView?.alpha = finalAlpha
            })
        { (completed) -> Void in
                if !self.presenting {
                    fromView?.removeFromSuperview()
                    self.tDelegate?.didDissmisedPresentedViewController()
                }
                let cancelled = transitionContext.transitionWasCancelled()
                transitionContext.completeTransition(!cancelled)
        }
    }

Xcode在此行显示错误:

guard let containerView = transitionContext.containerView() else { return }

5225
2018-06-29 17:50


起源

显示哪个错误? - Ulli H


答案:


transitionContext.containerView() 被更改为返回非可选,因此您无法使用它来初始化条件绑定中的变量,如 guard 要么 if let

你应该删除 guard 从那一行:

let containerView = transitionContext.containerView()

14
2018-06-29 18:20



谢谢。从保护条件中删除这段代码后,它现在正在工作。 - Farrakh Javed
使用上面的建议行我得到另一个错误,“不能调用非函数类型UIView的值;修复它:删除()”任何想法? - tylerSF


发生演示的容器视图。它是呈现和呈现视图控制器视图的祖先。

这个containerView被传递给动画控制器,它返回一个非可选的

注意:

if let / if var可选绑定仅在表达式右侧的结果是可选的时才起作用。如果右侧的结果不是可选的,则不能使用此可选绑定。这个可选绑定的要点是检查nil,如果它是非nil,则仅使用该变量。


2
2018-05-04 10:51