Swift自定义MDCCornerTreatment重写路径方法时radius属性取值异常求助
问题表现
在iOS项目中使用Material Design Components框架实现自定义角点逻辑时,出现属性值读取不一致的异常:
- 手动修改自定义角点类的
radius属性后,外部调用重写的路径生成方法时,可正确读取到修改后的属性值 - 框架内部自动调用路径生成方法时,读取到的
radius始终是类中定义的默认初始值12,与外部设置的值无关
复现代码
自定义角点类实现
class CustomCorner: MDCCornerTreatment{ var radius: CGFloat = 12 convenience init(radius: CGFloat) { self.init() self.radius = radius print("Corner initialized with radius=\(self.radius)") } override func pathGeneratorForCorner(withAngle angle: CGFloat) -> MDCPathGenerator { print("entering pathGeneratorForCorner with radius \(self.radius)") let path = MDCPathGenerator(start: CGPoint(x: 0, y: radius)) print("continuing pathGeneratorForCorner with radius \(radius)") path.addLine(to: CGPoint(x: radius, y: radius)) path.addLine(to: CGPoint(x: radius, y: 0)) return path } }
测试流程代码
let mdcRectangleShape = MDCRectangleShapeGenerator() let customCorner = CustomCorner(radius: 8) print("step 1 radio=\(customCorner.radius)") customCorner.radius = 5 print("step 2 radio=\(customCorner.radius)") mdcRectangleShape.setCorners(customCorner) print("step 3 radio=\(customCorner.radius)") let _ = customCorner.pathGeneratorForCorner(withAngle: 10) nextButton.shapeGenerator = mdcRectangleShape print("step 4 radio=\(customCorner.radius)")
运行输出结果
Corner initialized with radius=8.0
step 1 radio=8.0
step 2 radio=5.0
step 3 radio=5.0
entering pathGeneratorForCorner with radius 5.0
continuing pathGeneratorForCorner with radius 5.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
step 4 radio=5.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
entering pathGeneratorForCorner with radius 12.0
continuing pathGeneratorForCorner with radius 12.0
已尝试的无效排查方案
- 将radius值封装为自定义类持有,验证值类型传递逻辑,未解决问题
- 初始化完成后直接赋值修改radius属性,日志确认属性值已成功变更,但框架内部调用时仍读取初始值
- 在重写方法内通过
UnsafePointer读取属性值,仍得到默认初始值,符合“属性存在两份独立内存”的异常表象 - 尝试用
let声明radius属性,与便利初始化器逻辑冲突,触发初始化顺序相关编译错误 - 将radius设为可选值,异常现象无变化
问题根因
Material Design Components框架中所有形状生成相关的类型(包括MDCCornerTreatment)均遵守NSCopying协议。框架内部的setCorners方法、渲染流程不会直接持有外部传入的原始实例,而是通过调用copy()方法生成独立副本用于内部渲染,避免外部修改意外影响框架渲染状态。
当前自定义的CustomCorner类没有重写copy(with:)方法,复制流程会走NSObject的默认实现:调用类的无参init()方法生成新实例,这个新实例的radius会被设置为类中定义的默认值12,完全不会携带原实例上设置的radius值。这就解释了为什么外部持有原实例、修改属性后值是正确的,但框架内部调用路径生成方法时,拿到的是从未赋值radius的副本实例,读取到的是默认值。
手动调用pathGeneratorForCorner时能拿到正确值,是因为此时调用的是原始的customCorner实例,没有经过copy流程。
解决方案
重写CustomCorner类的copy(with:)方法,在复制时将原实例的radius值传递到新副本上:
override func copy(with zone: NSZone? = nil) -> Any { let copiedCorner = CustomCorner(radius: self.radius) return copiedCorner }
修改完成后,框架内部生成的副本实例就会携带正确的radius值,不会再读取到默认初始值。
内容的提问来源于stack exchange,提问作者Alfredo Meraz

