completely erasing UIBezierPath & CAShapeLayer drawn items
Asked Answered
R

3

8

In portrait mode (first time drawing)

enter image description here

After rotating to landscape, why are the older shapes:

enter image description here

I am using UIBezierPath & CAShapeLayer to draw circles & lines in a custom UIView's layer. The problem is inspite of being able to draw new circles & lines successfully after device is rotated, I am unable to remove the older drawn shapes. The new shapes drawn after device rotation are perfect, I just need to remove from screen those older shapes sticking in the screen. Images attached.

Radiotelephony answered 3/12, 2014 at 15:49 Comment(1)
Please include relevant code (e.g. what you do upon rotation).Funiculus
F
12

You can either remove the previous CAShapeLayer (with removeFromSuperlayer) or replace the path of the previous CAShapeLayer. You would appear to be adding a new layer without removing the old one, but of course it is impossible to tell without source code.

Funiculus answered 3/12, 2014 at 15:55 Comment(0)
R
4

aah! found the solution using rootView.layer.sublayers = nil removed all earlier shapes

Radiotelephony answered 4/12, 2014 at 9:52 Comment(1)
I'd glad your technique worked, but I'd still lean towards keeping your own weak references to the layers that you've added and then calling removeFromSuperlayer on them. This technique of setting sublayers to nil seems a little heavy-handed to me. Plus I've seen posts regarding there being problems with that technique (there might be some OS version variances in the behavior of this technique). Still, I'm glad you solved your problem.Funiculus
W
2

For me

rootView.layer.sublayers = nil

Threw a memory exception, but since I needed to remove just CAShapeLayer objects, the following code worked for me:

for sublayer in view.layer.sublayers! where sublayer is CAShapeLayer {
    sublayer.removeFromSuperlayer()
}
Wende answered 26/8, 2018 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.