我正在尝试为矩形实现动画以执行模态垂直滑动.但是,当我尝试编译代码时,我得到以下错误"Swift 4表达式类型'@value CGRect'是模糊的,没有更多上下文".我已将问题隔离到传递给CGRect init值的参数,但根据Apple的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)
})
}
Run Code Online (Sandbox Code Playgroud)
Ror*_*ior 14
我到这里寻找答案导致错误的原因"表达式类型'@lvalue CGRect'没有更多上下文是模棱两可的",虽然这里没有任何内容为我解答这个问题所以我确实找到了问题的解决方案所以我会留下它这里适用于通过Google偶然发现此问题的其他人.在创建CGRect时,请确保将任何不明确的值显式转换为CGFloats(例如var width = CGFloat(100)),而不是依赖编译器为您推断出正确的类型/强制转换.
小智 9
这是我将要报告的XCode 10.1中的错误。
let myHeight = CGFloat(100)
myHeight -= myView.frame.height
Run Code Online (Sandbox Code Playgroud)
产生错误:
“表达式类型'@lvalue CGRect'含糊不清,没有更多上下文”
错误是错误的,问题在于这myHeight是一个常数。
对我来说,我把它Int作为 CGRect height。
Xcode 错误地指向y了这个错误
表达式类型“@value CGRect”在没有更多上下文的情况下不明确
只是隐式地将高度定义为 CGFloat
let height:CGFloat = 100
Run Code Online (Sandbox Code Playgroud)
finalFrameForVC出现异常“'@value CGRect'在没有更多上下文的情况下是不明确的”不是你的,但它是fromView.view.frame。
通过显式声明为 UIView 来修复它fromView.view:
UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
let view: UIView = fromView.view
view.frame = finalFrameForVC as! CGRect
}) { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27613 次 |
| 最近记录: |