7

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.

Dan Fairaizl
  • 2,172
  • 2
  • 28
  • 31
ystack
  • 1,785
  • 12
  • 23

3 Answers3

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
4

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

ystack
  • 1,785
  • 12
  • 23
  • 5
    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. – Rob Dec 04 '14 at 11:31
1

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()
}
Pavle Mijatovic
  • 773
  • 10
  • 6