'CGPointMake' is unavailable in swift [duplicate]
Asked Answered
A

2

31

I have a Gradient class which I am trying to convert to Swift 3 but I get the following error

'CGPointMake' is unavailable in swift

for

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

Can anyone help me out with what I can use instead of CGPointMake

Here's the full class;

@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)
}
}
Avoirdupois answered 5/9, 2016 at 19:44 Comment(2)
First Google hit for 'CGPointMake' is unavailable in swift 3 ...Wheedle
@MartinR you can use CGPoint(x: view.bounds.midX, y: view.bounds.midY) instead.Singultus
M
85

In swift you can create a CGPoint in swifty way CGPoint(x: xPos, y:yPos). So change your CGPointMake(startX, startY) to CGPoint(x: startX, y: startY)

Mackey answered 5/9, 2016 at 19:45 Comment(0)
C
13

swift 3 emphasizes in use of named Parameters.

CGPoint Can be created like this.

let point = CGPoint(x: 0,y :0) // CGFloat, Double, Int
Ceuta answered 5/9, 2016 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.