1

My goal is to get shapes I can drag abound the view, but first am trying to figure out if this is the correct way to set the frame so that a CAShapeLAyer's frame is not all zeros and so I can later drag it with touch events.

Below is the code in my class (DrawOVerlay - DO) which is used for a UIView on my Viewcontroller:

 private func drawArc(user c: CGContext) {

    c.setLineCap(.round)
    c.setLineWidth(35.0)

    c.addArc(center: CGPoint(x:187.5,y:200), radius: 90.0, startAngle: -0.5 * .pi, endAngle: (-0.5 * .pi) + (3 / 2 * .pi ), clockwise: false)

    c.replacePathWithStrokedPath()

    let shape = CAShapeLayer()
    shape.frame = bounds
    shape.path = c.path
    shape.fillColor = UIColor.red.cgColor
    shape.strokeColor = UIColor.darkGray.cgColor
    shape.lineWidth = 1

    print("DO Bounds before:\(String(describing: shape.bounds))")
    print("DO Frame before:\(String(describing: shape.frame))")

    print("DO BoundingBox:\(String(describing: shape.path?.boundingBox))")

    shape.bounds = (shape.path?.boundingBox)!

    print("DO Bounds after:\(String(describing: shape.bounds))")
    print("DO Frame Width:\(String(describing: shape.frame))")

    self.layer.addSublayer(shape)
}

The print statements are giving me the correct coordinates for the shape's bounds and frame, but I'm new to Swift and not sure this is the proper way to go about it.

And then, how do I use this information with touch events to drag the shape layer without flickering?

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
Rillieux
  • 587
  • 9
  • 23

0 Answers0