当我尝试在自定义视图文件中设置Gradient时,我在XCode 8中遇到错误“方法不会覆盖其超类中的任何方法”。 代码是
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
当我删除覆盖时,会发生此错误 使用Objective-C选择器'layerClass'的方法'layerClass()'与来自超类'UIView'的'layerClass'的getter冲突,并使用相同的Objective-C选择器“
完整代码在这里:
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帮帮我
答案 0 :(得分:2)
layerClass
现在是一个getter,所以你必须覆盖getter:
override public class var layerClass: Swift.AnyClass {
get {
return CAGradientLayer.self
}
}