首页 > 解决方案 > CAShapeLayer 不能使用矩形路径?

问题描述

我画了一个虚线边框

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

运行时问题:

ACAShapeLayer与矩形、圆角矩形或椭圆形路径一起使用。相反,使用经过适当转换的普通 CALayer 和cornerRadiusset。

如何解决?

标签: ioscalayer

解决方案


使用图层的 shadowPath 而不是这样的路径:

shapeLayer.shadowPath = UIBezierPath(
  roundedRect: centerView.bounds,
  cornerRadius: 22
).cgPath

推荐阅读