尝试在自定义视图文件中设置Gradient时,在XCode 8中出现错误“方法不会从其 super class 覆盖任何方法”。
该代码是

override class func layerClass() -> AnyClass {
    return CAGradientLayer.self
}

当我删除覆盖时,会发生此错误
“具有Objective-C选择器'layerClass'的方法'layerClass()'与具有相同Objective-C选择器的 super class 'UIView'中的'layerClass'的吸气剂发生冲突”

完整代码在这里:
import UIKit

@IBDesignable

class CustomView: UIView {

override class func layerClass() -> AnyClass {
    return CAGradientLayer.self
}

var gradientLayer: CAGradientLayer {
    return layer as! CAGradientLayer
}

override func draw(_ rect: CGRect) {
    // Drawing code
    layer.masksToBounds = true
    layer.borderWidth = 10.0
    layer.borderColor = UIColor(red: 0.0, green: 64/225.0, blue: 128/225.0, alpha: 1.0).cgColor
    layer.cornerRadius = 20.0

    let startColor = UIColor(red: 102/225.0, green: 204/225.0, blue: 1.0, alpha: 1.0).cgColor
    let endColor = UIColor(red: 0.0, green: 128/225.0, blue: 1.0, alpha: 1.0).cgColor
    gradientLayer.colors = [startColor,endColor]
}
}

我是iOS plz的新手,请帮助我

最佳答案

layerClass现在是一个吸气剂,因此您必须覆盖该吸气剂:

override public class var layerClass: Swift.AnyClass {
    get {
         return CAGradientLayer.self
    }
}

10-06 07:46