Main.storyboard數組
ViewController.m學習
//動畫
// ViewController.matom
// 8A02.核心動畫 - CAKeyframeAnimationspa
//blog
// Created by huan on 16/2/4.圖片
// Copyright © 2016年 huanxi. All rights reserved.ip
//ci
#import "ViewController.h"animation
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//添加一個圓
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenW)];
circleView.backgroundColor = [UIColor yellowColor];
//設置圓角
circleView.layer.cornerRadius = screenW * 0.5;
[self.view addSubview:circleView];
//把圖片移到頂部
[self.view bringSubviewToFront:self.imageView];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//學習幀動畫
//建立一個幀動畫
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
//設置動畫執行路徑 指定四個點
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(250, 50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(250, 250)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(50, 250)];
//數組第一個是"開始狀態" 最後一個是「結束狀態」
animation.values = @[value1, value2, value3, value4, value1];
//設置時間
animation.duration = 3;
//設置動畫節奏
// kCAMediaTimingFunctionEaseIn 先慢後快
// kCAMediaTimingFunctionEaseOut 先慢後快
// kCAMediaTimingFunctionEaseOut 線性勻速
// kCAMediaTimingFunctionEaseInEaseOut 中間快兩邊慢
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
#warning 內部的path的優先級大於values優先級
//設置路徑
CGMutablePathRef path = CGPathCreateMutable();
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 0, screenW, screenW));
animation.path = path;
//c語言的數據類型,若是create/copy/retain 建立要釋放
//添加動畫
[self.imageView.layer addAnimation:animation forKey:nil];
}
@end
結果