Swift 4表达式类型'@value CGRect'在没有更多上下文的情况下是模糊的

问题描述 投票:27回答:11

我正在尝试为矩形实现动画以执行模态垂直滑动。但是,当我尝试编译代码时,我得到以下错误“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)
    })
}
swift cgrect
11个回答
33
投票

当我尝试将view.frame.height乘以Double时,我遇到了同样的错误。如果您在screenBounds属性上进行任何数学运算,请确保类型匹配。


0
投票

当我尝试创建CGRect并忘记将.constant添加到NSLayoutConstraint的变量名称末尾时,我收到此错误


0
投票

就像Rory Prior写的上面我得到这个错误试图制作一个CGPoint我需要指定参数是CGFloat但我还需要标记参数x:和y:进入CGPoint以便CGPoint知道期待CGFloat。这是代码:

let topRight = CGPoint(x:CGFloat(cellTitleLabel.frame.maxX),y:CGFloat(cellTitleLabel.frame.minY))

25
投票

我的问题是我试图在CGFloat变量和Double变量之间执行操作。

修复:将Double变量转换为CGFloat。检查变量类型。

我希望它有所帮助! :)


14
投票

我到这里寻找答案导致错误的原因“表达式类型'@lvalue CGRect'没有更多上下文是模棱两可的”,虽然这里没有任何内容为我解答这个问题所以我确实找到了问题的解决方案所以我会留下它这里适用于通过Google发现此问题的其他任何人。在创建CGRect时,请确保将任何不明确的值显式转换为CGFloats(例如var width = CGFloat(100)),而不是依靠编译器为您推断出正确的类型/强制转换。


8
投票

这是我即将报告的XCode 10.1中的一个错误。

let myHeight = CGFloat(100)

myHeight -= myView.frame.height

这会产生错误:

“表达式类型'@lvalue CGRect'含糊不清,没有更多上下文”

错误是错误的,问题是myHeight是一个常数。


4
投票

这不是你的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)
    }

2
投票

问题是你的动画调用

  1. 写得不正确
  2. 该方法不存在

重新格式化:

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)
   }
)

2
投票

对我来说,我把Int作为CGRect高度。

Xcode误将此错误指向y

表达式类型'@value CGRect'在没有更多上下文的情况下是不明确的

enter image description here

只是隐含地将高度定义为CGFloat

let height:CGFloat = 100

1
投票

我刚才遇到了同样的问题。我修复它像这样:

enter image description here

和上面的例子一样:self.view必须分配给另一个变量,ant应该强制转换视图:view!,然后编译将成功运行。


1
投票

真的很傻n00b回答,我有同样的问题

“表达式类型'@lvalue CGRect'含糊不清,没有更多上下文”

这是因为我在CGFloat之前按下了回归。检查以确保它在同一条线上。希望这有助于某人。

© www.soinside.com 2019 - 2024. All rights reserved.