Graphics Programs Reference
In-Depth Information
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
CABasicAnimation *ba = [CABasicAnimation anima-
tionWithKeyPath:@"position"];
[ba setFromValue:[NSValue valueWithCGPoint:[boxLayer position]]];
[ba setToValue:[NSValue valueWithCGPoint:p]];
[ba setDuration:3.0];
// Update the model layer
[boxLayer setPosition:p];
// Add animation that will gradually update presentation layer
[boxLayer addAnimation:ba forKey:@"foo"];
}
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
// UITouch *t = [touches anyObject];
// CGPoint p = [t locationInView:self];
// [CATransaction begin];
// [CATransaction setDisableActions:YES];
// [boxLayer setPosition:p];
// [CATransaction commit];
}
In this animation, If you build and run now and tap the screen, you will see the
boxLayer slowly move to wherever you touched the screen.
If you comment out the line that says [boxLayer setPosition:p] , you'll see
that the layer bounces back to its starting position once the animation ends. For be-
ginners, this is a very common error.
No animation happens. Your layer leaps directly to its final state. When an anim-
ation begins, if there is no fromValue , the fromValue is taken from the mod-
el layer. If you update the model to the final state before starting the animation,
your fromValue and toValue end up the same. Usually the fix is to give the
animation an explicit fromValue .
Search WWH ::




Custom Search