我有一个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)
}
}
答案 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