问题描述
我画了一个虚线边框
let centerView = UIView()
centerView.frame = CGRect(x: 100,y: 100,width: 80,height: 50)
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.gray.cgColor
shapeLayer.lineDashPattern = [3,2]
shapeLayer.linewidth = 0.5
shapeLayer.path = UIBezierPath(
roundedRect: centerView.bounds,cornerRadius: 22
).cgPath
view.addSubview(centerView)
centerView.layer.addSublayer(shapeLayer)
当我点击Debug View Hierarchy
运行时问题:
CAShapeLayer
与矩形,圆角矩形或椭圆形路径一起使用。相反,请使用经过适当转换且设置了cornerRadius
的普通CALayer。
如何解决?
解决方法
使用图层的 shadowPath 而不是 path 像这样:
shapeLayer.shadowPath = UIBezierPath(
roundedRect: centerView.bounds,cornerRadius: 22
).cgPath