This question already has answers here:
CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift

(10个回答)


4年前关闭。




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



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

最佳答案

您可以 swift 创建CGPoint CGPoint(x: xPos, y:yPos)
因此,将CGPointMake(startX, startY)更改为CGPoint(x: startX, y: startY)

关于ios - swift 无法使用“CGPointMake”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39336793/

10-14 08:50