将我的项目转换为Swift 3语法时,我遇到了几个错误,并且能够解决除了一个之外的所有错误。
我收到的错误消息是:
"Initializer for conditional binding must have Optional type, not 'UIView'"
以下是代码(我使用Yalatis cocoapod ColorMatchTabs):
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
self.transitionContext = transitionContext
guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to),
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let containerView = transitionContext.containerView else {
return
}
containerView.addSubview(toViewController.view)
let needShow = mode == .show
if !needShow {
containerView.addSubview(fromViewController.view)
}
let animatedViewController = needShow ? toViewController : fromViewController
let initialRect = CGRect(origin: startPoint, size: CGSize.zero)
let initialCircleMaskPath = UIBezierPath(ovalIn: initialRect)
let extremePoint = CGPoint(x: startPoint.x, y: animatedViewController.view.bounds.height)
let radius = hypot(extremePoint.x, extremePoint.y)
let finalCircleMaskPath = UIBezierPath(ovalIn: initialRect.insetBy(dx: -radius, dy: -radius))
let maskLayer = CAShapeLayer()
maskLayer.path = needShow ? finalCircleMaskPath.cgPath : initialCircleMaskPath.cgPath
animatedViewController.view.layer.mask = maskLayer
let maskLayerAnimation = CABasicAnimation(keyPath: "path")
maskLayerAnimation.fromValue = initialCircleMaskPath.cgPath
maskLayerAnimation.fromValue = needShow ? initialCircleMaskPath.cgPath : finalCircleMaskPath.cgPath
maskLayerAnimation.toValue = needShow ? finalCircleMaskPath.cgPath : initialCircleMaskPath.cgPath
maskLayerAnimation.duration = transitionDuration(using: transitionContext)
maskLayer.add(maskLayerAnimation, forKey: "path")
}
这是失败的:
guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to),
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let containerView = transitionContext.containerView else {
return
}
此处显示错误消息:
let containerView = transitionContext.containerView else {
return
}
我还不太擅长快速,显然不足以解决这个错误。我很感激能得到的所有帮助!
答案 0 :(得分:1)
transitionContext.containerView
不是可选的,你不需要把它放在一个guard-let语句中,因为它总是有一个值
替换它:
guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to),
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let containerView = transitionContext.containerView else {
return
}
与
guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to),
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) else {
return
}
let containerView = transitionContext.containerView