The Secret Life of a CALayer (Part 1): Accessing and Manipulating Layers (Xcode)

UIView, UIImageView and even UIImage offer such a plethora of options that it is easy to forget how much power is held within the seemingly humble and neglected CALayer class but there are still some important things that the CALayer class can provide, and often in simple and flexible ways that can solve a whole range of challenges.

Let's first start by accessing a layer and changing its colour in the viewDidLoad: of our view controller:

self.view.layer.backgroundColor = [UIColor blueColor].CGColor;

Immediately you'll see there's an error. This is because we've forgotten to do some important things:

(1) Double click on the blue project file in the left navigation bar (where all your files are located). This should take you to Targets and the Summary subsection, which you need to scroll down until you reach "Linked Frameworks and Libraries".


(2) Click the small plus button that you see under the stack of toolbox icons to add to your frameworks and add QuartzCore.framework



(3) Now add the following to the header (.h) file of your ViewController:

<QuartzCore/QuartzCore.h>

Now all should look the same as if you'd just written:

self.view.backgroundColor = [UIColor blueColor];

So why all the fuss?

Well here's one reason for the fuss:

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
     self.view.layer.transform=CATransform3DMakeRotation(180/M_PI*180,100, 100, 100);
}

Control of 3d rotations and transformations.


The Secret Life of a CALayer: Part 1 | Part 2 Part 3 | Part 4

Comments