Rotating CALayers around Y axis using Core Animation and Controlling it with Slider

ref:

http://stackoverflow.com/questions/3831867/trying-to-make-a-card-from-calayers-that-can-flip-over

http://www.binpress.com/tutorial/objectivec-lesson-13-keyvalue-coding/79

Setting up the Layers

Basically, we have two layers. One if the front with some text. The other is ‘back’ layer with some text, in which we place underneath the front.

However, you rotate the ‘back’ 180 degrees so that back’s text is facing the back. That way, when we rotate the whole thing, we can see the text of front and back.

But first, let’s make a container layer to contain the front and back layers. Notice that its a CATransformLayer. This a a layer that’s just for containing other layers (it can’t have things like backgroundColor or borderWidth). It does however maintain the true 3D relationship of its child layers (i.e., it doesn’t flatten them like a regular CALayer). So you can make two layers, flip one and offset its zPosition just a hair and put them both in a CATransformLayer and now you can flip this parent layer around and the child layers stay locked together and always render properly.

method that creates and returns the ‘back’ CALayer
Notice at the end that we flip this back layer 180 so that it faces the other way.

Now create the front layer. We leave front layer as is.

The basic set up. First we create the container layer. Then we add the back first, and then the front on top of that. Then, we have the self layer add this container layer. After that, we have a nice card where when we rotate this container layer, we can see the front and back as represented.

Create the Animation

valueForKeyPath or Key-Value Coding.

Accessing ivars is usually done through accessor methods or dot-notation. But as a means of indirection, there is another way—Key-Value Coding, or KVC. This is a means of setting and getting the values by using strings—specifically, NSString, and its corresponding methods, including the very useful stringWithFormat:.

The key is in fact the same name as your variable. It follows a specific sequence of steps:

1) First, it looks for a getter method with the same name. In the above example, it looks for the -name or -isName (for boolean values) methods; if they are found, it invokes that method and returns that result.
2) If the getter methods are not found, the method looks for an instance variable by that name: name or _name.

Hence we get the number for rotating around y. Then we say that we want the animation

Have the container layer add the Animation

We add the animation to our CATransformLayer and give it the name rotateYright.

Create the slider and add it to our self.view

Notice the minimum is set to 0 and max to 1. This is so that we can give decimal values from 0 to 1 and insert it into iVar timeOffset for our CATransformLayer container.

Implement the slider action method

then you set the slider value in decimal between 0 and 1 and feed it to CATransformLayer’s timeOffset. Hence, you can now control the animation of flipping a card around the Y axis.