How do you override layerClass in swift

后端 未结 4 1784
醉话见心
醉话见心 2021-02-04 23:45

In Objective-C we use to do it like this

+ (Class)layerClass
{
    return [CAEAGLLayer class];
}

Obviously this won\'t work:

CA         


        
4条回答
  •  旧时难觅i
    2021-02-05 00:33

    It can be written like this

    import UIKit
    
    class GradientView: UIView {
    
        override final class var layerClass: AnyClass {
            return CAGradientLayer.self
        }
    }
    

    Bonus tip: Maybe you would like to have a type safe getter to the layer? Then you can write

    import UIKit
    
    class GradientView: UIView {
    
        override final class var layerClass: AnyClass {
            return CAGradientLayer.self
        }
    
        override var layer: CAGradientLayer {
            return super.layer as! CAGradientLayer
        }
    
    }
    

提交回复
热议问题