1 @interface ViewController ()
2
3 @property (nonatomic, weak) IBOutlet UIView *containerView;
4 @property (nonatomic, strong) CALayer *shipLayer;
5
6 @end
7
8 @implementation ViewController
9
10 - (void)viewDidLoad
11 {
12 [super viewDidLoad];
13 //add the ship
14 self.shipLayer = [CALayer layer];
15 self.shipLayer.frame = CGRectMake(0, 0, 128, 128);
16 self.shipLayer.position = CGPointMake(150, 150);
17 self.shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;
18 [self.containerView.layer addSublayer:self.shipLayer];
19 }
20
21 - (IBAction)start
22 {
23 //animate the ship rotation
24 CABasicAnimation *animation = [CABasicAnimation animation];
25 animation.keyPath = @"transform.rotation";
26 animation.duration = 2.0;
27 animation.byValue = @(M_PI * 2);
28 animation.delegate = self;
29 [self.shipLayer addAnimation:animation forKey:@"rotateAnimation"];
30 }
31
32 - (IBAction)stop
33 {
34 [self.shipLayer removeAnimationForKey:@"rotateAnimation"];
35 }
36
37 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
38 {
39 //log that the animation stopped
40 NSLog(@"The animation stopped (finished: %@)", flag? @"YES": @"NO");
41 }
42
43 @end