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

39

我正在尝试为一个矩形实现模态垂直滑动的动画。然而,当我尝试编译代码时,我会得到以下错误:“Swift 4表达式类型“@value CGRect”在没有更多上下文的情况下是模棱两可的”。我已经将问题隔离到传递给CGRect init值的参数上,但根据苹果的iOS文档,这些参数应该足以指定我需要动画化的“frame”视图。

这是我的代码:

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

这个问题属于数据类型,我试图在CGFloat和Int类型之间进行减法运算,后来我将它们转换为相同的类型。例如:width: Int((filterView?.filterTblView.frame.width)!)-Int(width!) - Naresh
13个回答

0
像Rory Prior在上面所写的那样,我在尝试创建一个CGPoint时遇到了这个错误。我需要指定参数为CGFloat,但我还需要在进入CGPoint之前用x:和y:对参数加标签,这样CGPoint就知道要期望CGFloat。以下是代码:
let topRight = CGPoint(x:CGFloat(cellTitleLabel.frame.maxX),y:CGFloat(cellTitleLabel.frame.minY))

0

示例

myView.frame = CGRect(x: 0, y: 0, width: CGFloat(videoImaView.frame.width * CGFloat(0.4)), height: height)

0

明确声明要更改的变量的副本,然后将其赋值回去。

func clickButton() {
    UIView.animateWithDuration(animationDuration, animations: {
        let buttonFrame = self.button.frame
        buttonFrame.origin.y = buttonFrame.origin.y - 1.0
        self.button.frame = buttonFrame
    }
}

感谢Rishi在这里的回答: https://dev59.com/xo7da4cB1Zd3GeqP_lAQ#32133196


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接