Question

I have a CALayer that contains few other subLayers (CATextLayer actually)

I want to apply some transformation on that layer when user do usual gesture on the ipad but it doesn't seem to be working properly. The goal of using the CALayer was to apply the transformation only to that Layer so that all of my sub-textLayer would be affected at the same time with the same transformation.

What's happening is that the transformation seem to flicker between previous and current position. I don't really get what could be the problem... When I do a 2 fingers panning gesture for example, CaTextLayer positions flickers all the time during my gesture and at the end they are all correctly placed at their new translated position.

So everything seems to work fine except that flickering thing that is bothering me a lot.

Do I need to set some property I don't know about ? I'm thinking it might have something to do with bounds and frame too....

Here's how I create my CATextLayer (This is done only once at creation and it works properly):

_textString = [[NSString alloc] initWithString: text];   
_position = position;

attributedTextLayer_ = [[CATextLayer alloc] init];
attributedTextLayer_.bounds = frameSize;

//.. Set the font 

attributedTextLayer_.string = attrString;
attributedTextLayer_.wrapped = YES;

CFRange fitRange;
CGRect textDisplayRect = CGRectInset(attributedTextLayer_.bounds, 10.f, 10.f);
CGSize recommendedSize = [self suggestSizeAndFitRange:&fitRange 
                                      forAttributedString:attrString 
                                                usingSize:textDisplayRect.size];

[attributedTextLayer_ setValue:[NSValue valueWithCGSize:recommendedSize] forKeyPath:@"bounds.size"];
attributedTextLayer_.position = _position;

This is how I add them to my Super CALayer

[_layerMgr addSublayer:t.attributedTextLayer_];

[[_drawDelegate UI_GetViewController].view.layer addSublayer:_layerMgr];

And here's how I apply my transformation :

_layerMgr.transform = CATransform3DMakeAffineTransform(_transform);

Was it helpful?

Solution

After lots of reading and testing...I've found my own solution.

It looks like the CoreAnimation uses default animation when you do transformation or operation on any layers. It's very recommended that when you do such CALayer operation that you go through what they call "Transactions".

I've found all about that in the CoreAnimation Programming guide, under the section : transactions.

My solution was then to implement such a transaction, and preventing any animation while doing CALayer operation.

This is what I do when applying my transformation (which prevents flickering) :

-(void)applyTransform

{

if (! CGAffineTransformIsIdentity(_transform))

{

    [CATransaction begin];

    //This is what prevents all animation during the transaction
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];

    _layerMgr.transform = CATransform3DMakeAffineTransform(_transform);

    [CATransaction commit];
} 

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top