微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CAShapeLayer无法使用rect路径?

如何解决CAShapeLayer无法使用rect路径?

我画了一个虚线边框

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。