【发布时间】:2026-02-07 05:30:01
【问题描述】:
我想在 swift 中子类化 UIView,并使用 CAShapeLayer 作为该子类的层类型 - 用 layerClass() 覆盖层类型。
如何访问 CAShapeLayer 中而不是 CALayer 中的属性 - 例如下面示例中的路径。下面的代码无法编译,因为 path 不是 CALayer 的成员。
override class func layerClass() -> AnyClass {
return CAShapeLayer.self
}
override func awakeFromNib() {
var path: UIBezierPath = UIBezierPath.init(ovalInRect: CGRectMake(0, 0, 30, 10))
self.layer.path = path.CGPath
}
【问题讨论】: