1

I am trying to add a bezierPath as a sublayer of a view.

Following is my code

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)

Also, the attached image shows the result of my current implementation where the blue outlined box to the right is the sublayer and the LightGray box over Szpilman is the view to which I’m adding the sublayer

enter image description here

How can I add the sublayer exactly into the gray box?

halfer
  • 19,824
  • 17
  • 99
  • 186
Vaibhav Jhaveri
  • 1,579
  • 3
  • 28
  • 53
  • 1
    just check what arrPoints ocntain. It should start from 0,0 and end there. I guess it might be because of the frame reference. If its not that you might have to subtract origin.x and origin.y from each point. – Mohammad Sadiq Jan 09 '19 at 10:20
  • @MohammadSadiq you're solution worked. Thanks a ton!!! – Vaibhav Jhaveri Jan 09 '19 at 10:44

0 Answers0