首页 > 解决方案 > '(CGFloat, CGFloat) -> CGRect' 类型的值没有成员'origin'

问题描述

我正在尝试使用CGRect.insetBy( dx : ... , dy : ...),但代码给了我一个根本无法解决的错误。

func scaleRect (rect: CGRect , xScale : CGFloat, yScale : CGFloat , offset : CGPoint) -> (CGRect){
    let width = rect.width
    let height = rect.height
    let scaleUp : CGFloat = 2.0

    var newWidth  = sqrt(width * width * scaleUp)
    var newHeight = sqrt(height * height * scaleUp)


    var newRect = rect.insetBy(dx: (width - newWidth)/2, dy: (height - newHeight)/2)
          // error here ***  Value of type '(CGFloat, CGFloat) -> CGRect' has no member 'origin'

    var resultRect = CGRect(x: newRect.origin.x, y: newRect.origin.y, width: newRect.size.width, height: newRect.size.height)

    resultRect = CGRect.offsetBy(resultRect, offset.x , offset.y)
    return resultRect
}

标签: iosswift

解决方案


offsetBy根据 Swift,你应该像这样使用,

let rect = resultRect.offsetBy(dx: offset.x, dy: offset.y)

了解有关insetByoffsetByCGGeometry 的更多信息


推荐阅读