Blog Archives

Another CoreAnimation Protip

If you’re building your interface using CALayers, beware the following weirdness when sublayers are added to a parent layer.  I had a parent layer, with a bunch of sublayers being added and removed during the lifetime of the view.  I didn’t want any animations when that occurs, but I was still noticing a fade in / fade out action going on.

I had already disabled the kCAOrderIn and kCAOrderOut action keys on the sublayers, but I didn’t realize I also had to disable the sublayers action on the parent layer.  Beware that the sublayers action is also fired when you call [CALayer addSublayer:] and [CALayer removeFromSuperlayer].

Advertisements

Objective-C Quickie – Block variables

Blocks are becoming more and more preferred in iOS and Objective-c, with most of the UIView animations becoming block based and methods added to the NS collection classes to use blocks for enumeration. Blocks can also be stored as variables and used over and over again. This is immensely useful when subscribing to a DRY method of coding.

Below is some code I’m using to apply transformations to CALayers. I’ve got three sets of them, and since I simply want to apply the same transformation to them, I can write the block for creating and applying the transformation once, and use it on all three sets:

// this is a block variable
id transBlock = ^(id obj, BOOL *stop)
{
    CALayer *layer = (CALayer *)obj;
    CGAffineTransform trans = CGAffineTransformConcat([layer affineTransform],                                                       CGAffineTransformMakeTranslation(delta * 0.7, 0));
    [layer setAffineTransform:trans];
};

[displayedBars enumerateObjectsUsingBlock:transBlock];
[displayedBarLabels enumerateObjectsUsingBlock:transBlock];
[displayedAxisLabels enumerateObjectsUsingBlock:transBlock];