Implicitly Animatable Properties

Several of the properties of CALayer are implicitly animatable. This means that changes to these properties are automatically animated when their setters are called. The property position is an implicitly animatable property. Sending the message setPosition: to a CALayer doesn’t just move the layer to a new position; it animates the change from the old position to the new one.

In this section, you will have the application respond to user taps: the boxLayer will move to wherever the user starts a touch. This change in position will be animated because position is an implicitly animatable property.

In HypnosisView.m, implement touchesBegan:withEvent: to change the layer’s position.

-​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​B​e​g​a​n​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​
{​
 ​ ​ ​ ​U​I​T​o​u​c​h​ ​*​t​ ​=​ ​[​t​o​u​c​h​e​s​ ​a​n​y​O​b​j​e​c​t​]​;​
 ​ ​ ​ ​C​G​P​o​i​n​t​ ​p​ ​=​ ​[​t​ ​l​o​c​a​t​i​o​n​I​n​V​i​e​w​:​s​e​l​f​]​;​
 ​ ​ ​ ​[​b​o​x​L​a​y​e​r​ ​s​e​t​P​o​s​i​t​i​o​n​:​p​]​;​
}​

Build and run the application. The layer will move smoothly from its current position to where you tap. All you had to do to get this animation was send setPosition:. Pretty cool, huh?

If the user drags rather than taps, let’s have the layer follow the user’s finger. In HypnosisView.m, implement touchesMoved:withEvent: to send setPosition: to the layer. method:

-​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​M​o​v​e​d​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​
{​
 ​ ​ ​ ​U​I​T​o​u​c​h​ ​*​t​ ​=​ ​[​t​o​u​c​h​e​s​ ​a​n​y​O​b​j​e​c​t​]​;​
 ​ ​ ​ ​C​G​P​o​i​n​t​ ​p​ ​=​ ​[​t​ ​l​o​c​a​t​i​o​n​I​n​V​i​e​w​:​s​e​l​f​]​;​
 ​ ​ ​ ​[​b​o​x​L​a​y​e​r​ ​s​e​t​P​o​s​i​t​i​o​n​:​p​]​;​
}​

Build and run the application. This is not so cool. Notice how the animation makes the layer lag behind your dragging finger, making the application seem sluggish.

Why the poor response? An implicitly animatable property changes to its destination value over a constant time interval. However, changing a property while it is being animated starts another implicit animation. Therefore, if a layer is in the middle of traveling from point A to point B, and you tell it to go to point C, it will never reach B; and that little change of direction coupled with the timer restarting makes the animation seem choppy. (Figure 22.7)

Figure 22.7  Animation missing waypoints

Animation missing waypoints

To disable an implicit animation, you can use an animation transaction. Animation transactions allow you to batch animations and set their parameters, like the duration and animation curve. To begin a transaction, you send the message begin to the class CATransaction. To end a transaction, you send commit to CATransaction. Within the begin and commit block, you can set properties of a layer and also set values for the transaction as a whole.

Animation transactions can be used for lots of things, but here we’ll use it to disable the animation of the layer’s change in position. In HypnosisView.m, edit touchesMoved:withEvent:.

-​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​M​o​v​e​d​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​
{​
 ​ ​ ​ ​U​I​T​o​u​c​h​ ​*​t​ ​=​ ​[​t​o​u​c​h​e​s​ ​a​n​y​O​b​j​e​c​t​]​;​
 ​ ​ ​ ​C​G​P​o​i​n​t​ ​p​ ​=​ ​[​t​ ​l​o​c​a​t​i​o​n​I​n​V​i​e​w​:​s​e​l​f​]​;​
 ​ ​ ​ ​[​C​A​T​r​a​n​s​a​c​t​i​o​n​ ​b​e​g​i​n​]​;​
 ​ ​ ​ ​[​C​A​T​r​a​n​s​a​c​t​i​o​n​ ​s​e​t​D​i​s​a​b​l​e​A​c​t​i​o​n​s​:​Y​E​S​]​;​
 ​ ​ ​ ​[​b​o​x​L​a​y​e​r​ ​s​e​t​P​o​s​i​t​i​o​n​:​p​]​;​
 ​ ​ ​ ​[​C​A​T​r​a​n​s​a​c​t​i​o​n​ ​c​o​m​m​i​t​]​;​
}​

Build and run the application. Now the dragging should feel much more responsive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.31.165