我正在尝试为矩形实现一个动画,以执行模态垂直滑动。然而,当我试图编译代码时,我得到了以下错误“Swift 4表达式类型'@value CGRect'在没有更多上下文的情况下不明确”。我已经将问题与传递给CGRect init值的参数隔离开来,但根据苹果的iOS文档,这些参数应该足以指定我需要设置动画的“框架”视图。
这是我的代码:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard
let fromView = transitionContext.viewController(forKey: .from),
let toView = transitionContext.viewController(forKey: .to)
else {
return
}
let containerView = transitionContext.containerView
containerView.insertSubview((toView.view)!, belowSubview: (fromView.view)!)
let toFrame = transitionContext.finalFrame(for: toView)
let screenBounds = UIScreen.main.bounds
let bottomLeftCorner = CGPoint(x: 0, y: screenBounds.height)
let finalFrameForVC = CGRect(origin: bottomLeftCorner, size: screenBounds.size)
UIView.animate(withDuration: 2, delay: 1,
options: [], (using: transitionContext.finalFrame(for: fromView)),
animations: {
fromView.view.frame = finalFrameForVC
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
当我尝试将view.frame.height乘以Double时,我遇到了同样的错误。如果要对 screenBounds 属性进行任何数学运算,请确保类型匹配。
我的问题是我试图在一个CGFloat变量和Double变量之间执行一个操作。
修复:将Double变量转换为CGFloat。检查变量类型。
希望有帮助!:)
我来到这里寻找导致错误“表达式类型'@lvalue CGRect'在没有更多上下文的情况下模棱两可”的答案,虽然这里没有为我回答这个问题,但我确实找到了问题的解决方案,所以我会把它留给任何其他通过谷歌偶然发现这个问题的人。在创建 CGRect 时,请确保将任何不明确的值显式转换为 CGFloat(例如 var width = CGFloat(100)),而不是依赖编译器为您推断正确的类型/转换。