Graphics Reference
In-Depth Information
Listing 8.9 Animating the Virtual transform.rotation Property
@interface ViewController ()
@property ( nonatomic , weak ) IBOutlet UIView *containerView;
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//add the ship
CALayer *shipLayer = [ CALayer layer ];
shipLayer. frame = CGRectMake ( 0 , 0 , 128 , 128 );
shipLayer. position = CGPointMake ( 150 , 150 );
shipLayer. contents = ( __bridge id )[ UIImage imageNamed :
@"Ship.png" ]. CGImage ;
[ self .containerView. layer addSublayer :shipLayer];
//animate the ship rotation
CABasicAnimation *animation = [ CABasicAnimation animation ];
animation. keyPath = @"transform.rotation" ;
animation. duration = 2.0 ;
animation. byValue = @( M_PI * 2) ;
[shipLayer addAnimation :animation forKey : nil ];
}
@end
This approach works great . The benefits of animating transform.rotation instead of the
transform are as follows:
It allows us to rotate more than 180 degrees in a single step, without using keyframes.
It allows us to perform a relative rather than absolute rotation (by setting the byValue
instead of toValue ).
It allows us to specify the angle as a simple numeric value instead of constructing a
CATransform3D .
It won't conflict with transform.position or transform.scale (which are also
individually animatable using key paths).
Search WWH ::




Custom Search