我正在尝试为矩形实现动画以执行模态垂直滑动。但是,当我尝试编译代码时,我得到以下错误“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)
})
}
当我尝试将view.frame.height乘以Double时,我遇到了同样的错误。如果您在screenBounds属性上进行任何数学运算,请确保类型匹配。
当我尝试创建CGRect
并忘记将.constant
添加到NSLayoutConstraint
的变量名称末尾时,我收到此错误
就像Rory Prior写的上面我得到这个错误试图制作一个CGPoint
我需要指定参数是CGFloat
但我还需要标记参数x:和y:进入CGPoint
以便CGPoint
知道期待CGFloat
。这是代码:
let topRight = CGPoint(x:CGFloat(cellTitleLabel.frame.maxX),y:CGFloat(cellTitleLabel.frame.minY))
我的问题是我试图在CGFloat变量和Double变量之间执行操作。
修复:将Double变量转换为CGFloat。检查变量类型。
我希望它有所帮助! :)
我到这里寻找答案导致错误的原因“表达式类型'@lvalue CGRect'没有更多上下文是模棱两可的”,虽然这里没有任何内容为我解答这个问题所以我确实找到了问题的解决方案所以我会留下它这里适用于通过Google发现此问题的其他任何人。在创建CGRect时,请确保将任何不明确的值显式转换为CGFloats(例如var width = CGFloat(100)),而不是依靠编译器为您推断出正确的类型/强制转换。
这是我即将报告的XCode 10.1中的一个错误。
let myHeight = CGFloat(100)
myHeight -= myView.frame.height
这会产生错误:
“表达式类型'@lvalue CGRect'含糊不清,没有更多上下文”
错误是错误的,问题是myHeight
是一个常数。
这不是你的finalFrameForVC
上升例外“'@value CGRect'在没有更多背景的情况下是模棱两可的”但它是fromView.view.frame
。
通过将fromView.view
明确声明为UIView来修复它:
UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
let view: UIView = fromView.view
view.frame = finalFrameForVC as! CGRect
}) { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
问题是你的动画调用
重新格式化:
UIView.animate(
withDuration: 2,
delay: 1,
options: [],
(using: transitionContext.finalFrame(for: fromView)),
animations: {
fromView.view.frame = finalFrameForVC
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
注意(using: transitionContext.finalFrame(for: fromView)),
。您不能将方法的一部分包括在括号中,包括一个外部参数名称,它必须是:
UIView.animate(
withDuration: 2,
delay: 1,
options: [],
using: transitionContext.finalFrame(for: fromView),
animations: {
fromView.view.frame = finalFrameForVC
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
这就是导致错误的原因。但是,using:
部分也没有动画方法,因此我认为它只是一个复制粘贴错误,你实际上只想:
UIView.animate(
withDuration: 2,
delay: 1,
options: [],
animations: {
fromView.view.frame = finalFrameForVC
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
真的很傻n00b回答,我有同样的问题
“表达式类型'@lvalue CGRect'含糊不清,没有更多上下文”
这是因为我在CGFloat
之前按下了回归。检查以确保它在同一条线上。希望这有助于某人。