7
votes

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.

3
Please include relevant code (e.g. what you do upon rotation). - Rob

3 Answers

12
votes

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.

4
votes

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

1
votes

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()
}