Graphics Reference
In-Depth Information
The code for the swinging door is shown in Listing 9.2. We've simply animated the
outward swing and used the autoreverses property to make the door swing back
automatically. In this case, we've set repeatDuration to INFINITY so that the animation
plays indefinitely, although setting repeatCount to INFINITY would have had the same
effect. Note that the repeatCount and repeatDuration properties could potentially
contradict each other, so you should only specify a nonzero value for either one or the
other. The behavior if both properties are nonzero is undefined.
Listing 9.2 Swinging Door Implemented Using the autoreverses Property
@interface ViewController ()
@property ( nonatomic , weak ) UIView *containerView;
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//add the door
CALayer *doorLayer = [ CALayer layer ];
doorLayer. frame = CGRectMake ( 0 , 0 , 128 , 256 );
doorLayer. position = CGPointMake ( 150 - 64 , 150 );
doorLayer. anchorPoint = CGPointMake ( 0 , 0.5 );
doorLayer. contents = ( __bridge id )[ UIImage imageNamed :
@"Door.png" ]. CGImage ;
[ self .containerView. layer addSublayer :doorLayer];
//apply perspective transform
CATransform3D perspective = CATransform3DIdentity ;
perspective. m34 = - 1.0 / 500.0 ;
self .containerView. layer . sublayerTransform = perspective;
//apply swinging animation
CABasicAnimation *animation = [ CABasicAnimation animation ];
animation. keyPath = @"transform.rotation.y" ;
animation. toValue = @( - M_PI_2 ) ;
animation. duration = 2.0 ;
animation. repeatDuration = INFINITY ;
animation. autoreverses = YES ;
[doorLayer addAnimation :animation forKey : nil ];
Search WWH ::




Custom Search