1 @interface ViewController ()
2
3 @property (nonatomic, weak) IBOutlet UIView *containerView;
4 @property (nonatomic, weak) IBOutlet UILabel *speedLabel;
5 @property (nonatomic, weak) IBOutlet UILabel *timeOffsetLabel;
6 @property (nonatomic, weak) IBOutlet UISlider *speedSlider;
7 @property (nonatomic, weak) IBOutlet UISlider *timeOffsetSlider;
8 @property (nonatomic, strong) UIBezierPath *bezierPath;
9 @property (nonatomic, strong) CALayer *shipLayer;
10
11 @end
12
13 @implementation ViewController
14
15 - (void)viewDidLoad
16 {
17 [super viewDidLoad];
18 //create a path
19 self.bezierPath = [[UIBezierPath alloc] init];
20 [self.bezierPath moveToPoint:CGPointMake(0, 150)];
21 [self.bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)];
22 //draw the path using a CAShapeLayer
23 CAShapeLayer *pathLayer = [CAShapeLayer layer];
24 pathLayer.path = self.bezierPath.CGPath;
25 pathLayer.fillColor = [UIColor clearColor].CGColor;
26 pathLayer.strokeColor = [UIColor redColor].CGColor;
27 pathLayer.lineWidth = 3.0f;
28 [self.containerView.layer addSublayer:pathLayer];
29 //add the ship
30 self.shipLayer = [CALayer layer];
31 self.shipLayer.frame = CGRectMake(0, 0, 64, 64);
32 self.shipLayer.position = CGPointMake(0, 150);
33 self.shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;
34 [self.containerView.layer addSublayer:self.shipLayer];
35 //set initial values
36 [self updateSliders];
37 }
38
39 - (IBAction)updateSliders
40 {
41 CFTimeInterval timeOffset = self.timeOffsetSlider.value;
42 self.timeOffsetLabel.text = [NSString stringWithFormat:@"%0.2f", timeOffset];
43 float speed = self.speedSlider.value;
44 self.speedLabel.text = [NSString stringWithFormat:@"%0.2f", speed];
45 }
46
47 - (IBAction)play
48 {
49 //create the keyframe animation
50 CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
51 animation.keyPath = @"position";
52 animation.timeOffset = self.timeOffsetSlider.value;
53 animation.speed = self.speedSlider.value;
54 animation.duration = 1.0;
55 animation.path = self.bezierPath.CGPath;
56 animation.rotationMode = kCAAnimationRotateAuto;
57 animation.removedOnCompletion = NO;
58 [self.shipLayer addAnimation:animation forKey:@"slide"];
59 }
60
61 @end