1 @interface ViewController ()
2
3 @property (nonatomic, weak) IBOutlet UIView *containerView;
4 @property (nonatomic, weak) IBOutlet UITextField *durationField;
5 @property (nonatomic, weak) IBOutlet UITextField *repeatField;
6 @property (nonatomic, weak) IBOutlet UIButton *startButton;
7 @property (nonatomic, strong) CALayer *shipLayer;
8
9 @end
10
11 @implementation ViewController
12
13 - (void)viewDidLoad
14 {
15 [super viewDidLoad];
16 //add the ship
17 self.shipLayer = [CALayer layer];
18 self.shipLayer.frame = CGRectMake(0, 0, 128, 128);
19 self.shipLayer.position = CGPointMake(150, 150);
20 self.shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;
21 [self.containerView.layer addSublayer:self.shipLayer];
22 }
23
24 - (void)setControlsEnabled:(BOOL)enabled
25 {
26 for (UIControl *control in @[self.durationField, self.repeatField, self.startButton]) {
27 control.enabled = enabled;
28 control.alpha = enabled? 1.0f: 0.25f;
29 }
30 }
31
32 - (IBAction)hideKeyboard
33 {
34 ?[self.durationField resignFirstResponder];
35 [self.repeatField resignFirstResponder];
36 }
37
38 - (IBAction)start
39 {
40 CFTimeInterval duration = [self.durationField.text doubleva lue];
41 float repeatCount = [self.repeatField.text floatValue];
42 //animate the ship rotation
43 CABasicAnimation *animation = [CABasicAnimation animation];
44 animation.keyPath = @"transform.rotation";
45 animation.duration = duration;
46 animation.repeatCount = repeatCount;
47 animation.byValue = @(M_PI * 2);
48 animation.delegate = self;
49 [self.shipLayer addAnimation:animation forKey:@"rotateAnimation"];
50 //disable controls
51 [self setControlsEnabled:NO];
52 }
53
54 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
55 {
56 //reenable controls
57 [self setControlsEnabled:YES];
58 }
59
60 @end