Alexey Blinov
Alexey Blinov

Reputation: 1694

CALayer flickers when drawing a path

I am using a CALayer to display a path via drawLayer:inContext delegate method, which resides in the view controller of the view that the layer belongs to. Each time the user moves their finger on the screen the path is updated and the layer is redrawn. However, the drawing doesn't keep up with the touches: there is always a slight lag in displaying the last two points of the path. It also flickers, but only while displaying the last two-three points again. If I just do the drawing in the view's drawRect, it works fine and the drawing is definitely fast enough.

Does anyone know why it behaves like this? I suspect it is something to do with the layer buffering, but I couldn't find any documentation about it.

Upvotes: 0

Views: 1880

Answers (3)

alfwatt
alfwatt

Reputation: 2020

You might have better luck using a layer hosting view.

Instead of using the drawLayer:inContext: method, setup the view you want and add a CALayer to it:

UIView *layerHosting = [[UIView alloc] initWithFrame:frame];
[layerHosting setLayer:[[CALayer new] autorelease]];

Hope that helps!

Upvotes: 0

Ishank
Ishank

Reputation: 2926

Give the following before setNeedsDisplay method::

[CATransaction begin];

[CATransaction setValue:[NSNumber numberWithFloat:someDelay] forKey:kCATransactionAnimationDuration];

[aLayer setNeedsDisplay];                    
[CATransaction commit];

Upvotes: 1

user321782
user321782

Reputation: 11

[UIView new] is simply shorthand for [[UIView alloc] init].

Upvotes: 1

Related Questions