'CGPointMake'在swift中不可用

时间:2016-09-05 19:44:44

标签: ios swift swift3

我有一个Gradient类,我试图转换为Swift 3但是我收到以下错误

  

'CGPointMake'在swift

中不可用

代表

func configureGradientView() {

    let color1 = topColor ?? self.tintColor as UIColor
    let color2 = bottomColor ?? UIColor.black as UIColor
    let colors: Array <AnyObject> = [ color1.cgColor, color2.cgColor ]
    let layer = self.layer as! CAGradientLayer

    layer.colors = colors
    layer.startPoint = CGPointMake(startX, startY)
    layer.endPoint = CGPointMake(endX, endY)
}

任何人都可以帮我解决我可以使用的问题,而不是CGPointMake

这是全班;

@IBDesignable public class XGradientView: UIView {

@IBInspectable public var topColor: UIColor? {
    didSet {
        configureGradientView()
    }
}
@IBInspectable public var bottomColor: UIColor? {
    didSet {
        configureGradientView()
    }
}
@IBInspectable var startX: CGFloat = 0.0 {
    didSet{
        configureGradientView()
    }
}
@IBInspectable var startY: CGFloat = 1.0 {
    didSet{
        configureGradientView()
    }
}
@IBInspectable var endX: CGFloat = 0.0 {
    didSet{
        configureGradientView()
    }
}
@IBInspectable var endY: CGFloat = 0.0 {
    didSet{
        configureGradientView()
    }
}

public class func layeredClass() -> AnyClass {
    return CAGradientLayer.self
}

public required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    configureGradientView()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    configureGradientView()
}

public override func tintColorDidChange() {
    super.tintColorDidChange()
    configureGradientView()
}

func configureGradientView() {

    let color1 = topColor ?? self.tintColor as UIColor
    let color2 = bottomColor ?? UIColor.black as UIColor
    let colors: Array <AnyObject> = [ color1.cgColor, color2.cgColor ]
    let layer = self.layer as! CAGradientLayer

    layer.colors = colors
    layer.startPoint = CGPointMake(startX, startY)
    layer.endPoint = CGPointMake(endX, endY)
}
}

2 个答案:

答案 0 :(得分:67)

在swift中,您可以以快捷的方式创建CGPoint CGPoint(x: xPos, y:yPos)。 因此,将CGPointMake(startX, startY)更改为CGPoint(x: startX, y: startY)

答案 1 :(得分:13)

swift 3强调使用命名参数。

CGPoint可以像这样创建。

let point = CGPoint(x: 0,y :0) // CGFloat, Double, Int