在iOS Swift中将UIBezierPath添加到CAShapeLayer

时间:2019-01-09 09:29:32

标签: ios swift uibezierpath cashapelayer

我正在尝试添加bezierPath作为视图的子层。

以下是我的代码

let arrPoints = Array(picture.points)

let bezierPath = UIBezierPath()

bezierPath.move(to: CGPoint(x: (arrPoints.first?.x.cgFloat()!)! * scaleX,
                            y: (arrPoints.first?.y.cgFloat()!)! * scaleY))

for index in 1...(arrPoints.count - 1) {
    let point = arrPoints[index]
    bezierPath.addLine(to: CGPoint(x: point.x.cgFloat()! * scaleX,
                                   y: point.y.cgFloat()! * scaleY))
}

let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.strokeColor = UIColor(hexString: picture.color)!.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = picture.brushSize.cgFloat()!

let viewSymbol = SVGImageView(frame: CGRect(origin: origin, size: size))
viewSymbol.accessibilityHint = "Brush"
viewSymbol.isUserInteractionEnabled = true
viewSymbol.alpha = picture.alpha.cgFloat()!
viewSymbol.layer.insertSublayer(shapeLayer, at: 0)
viewSymbol.backgroundColor = UIColor(hexString: "#dfdfdf", transparency: 0.25)

viewLayerActive!.addSubview(viewSymbol)

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
viewSymbol.addGestureRecognizer(panGesture)

此外,所附图像显示了我当前实施的结果,其中右侧的蓝色轮廓框是子层,Szpilman上方的LightGray框是我要向其添加子层的视图

enter image description here

如何将子图层完全添加到灰色框中?

0 个答案:

没有答案