0

In my prototype app there are around 100 CALayers at different but fixed positions with the same small image as the content. The only thing necessary now is to toggle the hidden property repeatedly and very quickly.

This works, but it's noticably slower than with my previous approach using UIImage's drawAtPoint: method in drawRect.

I want a strobe-like look, no transitions. That's why I'm using hidden and not opacity, but still, it kind of looks like there's a fade and that tells me it's slow. With the drawAtPoint:-approach it looked good, but it was heavy on the CPU.

Fo this reason I rewrote it using CALayer and now I'm puzzled why it is that slow.

Can you give me advice how to investigate this? With Instruments I didn't gain any insight. It tells me it's rendering at 59-60 FPS but visibly it's much slower.

It seems like there's a delay between the (touch) events and the hiding or showing of the layers taking effect.


That's how I'm initializing the layers:

layers[i] = [CALayer layer];
layers[i].frame = frameForLayer(i);
layers[i].contents = (__bridge id)image;
[layers[i] setContentsScale:scale];
layers[i].hidden = YES;
[[self layer] addSublayer:layers[i]];

All that in the awakeFromNib in my main view.
Later, only the hidden properties are modified, the rest stays.


EDIT: Instead of just the someLayer.hidden = NO, I'm now writing

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
someLayer.hidden = NO;
[CATransaction commit];
Phantrast
  • 639
  • 1
  • 9
  • 18
  • 5
    Disable implicit animations: http://stackoverflow.com/questions/5833488/how-to-disable-calayer-implicit-animations – isaac Mar 28 '13 at 21:37

1 Answers1

2

try doing the above code in a CATransaction block and set the animation duration like so:

[CATransaction setValue:[NSNumber numberWithInt:0] forKey:kCATransactionAnimationDuration];

you may also need to do disable transitions like so:

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

I believe CALayers have a default 'animations' for when you set their contents.

Kelly Bennett
  • 725
  • 5
  • 27