Custom UIView using CALayers disappears after 180º rotation or navigation controller pop
- by Steve Madsen
I have a created a custom UIView subclass that is exhibiting some strange behavior. It is a spinning wheel selector, and for performance reasons it is drawn entirely into two CALayer instances. The bottom layer is the wheel itself, which is rotated using setAffineTransform: according to touches. The top layer is eye candy.
drawRect: is fairly simple. If the control hasn't been drawn yet (or it's been invalidated), it calls a method that creates the images and assigns them to the layer contents property.
- (void) drawRect:(CGRect)rect
{
if (imageLayer == nil)
{
[self drawIntoImageLayer];
}
[self updateWheelRotation];
}
When the view controller using this view first appears, everything is fine. There are two instances where the view completely disappears, however:
If the device is rotated a full 180°.
After a view controller is popped off the navigation stack and the view becomes visible again.
drawRect: is not called either time. Interestingly enough, it IS called after a 90° orientation change, and that causes the view to re-appear.
How can I ensure that a custom view using CALayers is redrawn properly in these situations?