IOS-CoreAnimation(核心動畫)

1、核心動畫數組

1.Core Animation是一組很是強大的動畫處理API,使用它能作出很是炫麗的動畫效果,並且每每是事半功倍,使用它須要先添加QuartzCore.framework和引入對應的框架<QuartzCore/QuartzCore.h>
 
2.開發步驟:
①初始化一個動畫對象(CAAnimation)並設置一些動畫相關屬性
②添加動畫對象到層(CALayer)中,開始執行動畫
 
3.CALayer中不少屬性均可以經過CAAnimation實現動畫效果,包括:opacity、position、transform、bounds、contents等(能夠在API文檔中搜索:CALayer Animatable Properties)
 
4.經過調用CALayer的addAnimation:forKey增長動畫到層(CALayer)中,這樣就能觸發動畫了。經過調用removeAnimationForKey能夠中止層中的動畫
 
5.Core Animation的動畫執行過程都是在後臺操做的,不會阻塞主線程
 
2、CAAnimation繼承結構
3、CAAnimation
 
1.全部動畫對象的父類,負責控制動畫的持續時間和速度,是個抽象類,不能直接使用,應該使用它具體的子類
 
2.屬性解析:(紅色表明來自CAMediaTiming協議的屬性)
duration:動畫的持續時間
repeatCount:動畫的重複次數
repeatDuration:動畫的重複時間
removedOnCompletion:默認爲YES,表明動畫執行完畢後就從圖層上移除,圖形會恢復到動畫執行前的狀態。若是想讓圖層保持顯示動畫執行後的狀態,那就設置爲  
                                   NO,不過還要設置fillMode爲kCAFillModeForwards 
fillMode:決定當前對象在非active時間段的行爲.好比動畫開始以前,動畫結束以後
beginTime:能夠用來設置動畫延遲執行時間,若想延遲2s,就設置爲CACurrentMediaTime()+2,CACurrentMediaTime()爲圖層的當前時間
timingFunction:速度控制函數,控制動畫運行的節奏
delegate:動畫代理
 
4、CAPropertyAnimation
1.是CAAnimation的子類,也是個抽象類,要想建立動畫對象,應該使用它的兩個子類:CABasicAnimation和CAKeyframeAnimation
 
2.屬性解析:
keyPath:經過指定CALayer的一個屬性名稱爲keyPath(NSString類型),而且對CALayer的這個屬性的值進行修改,達到相應的動畫效果。好比,指定@」position」爲keyPath,就修改CALayer的position屬性的值,以達到平移的動畫效果
 
5、CABasicAnimation
1.CAPropertyAnimation的子類
 
2.屬性解析:
fromValue:keyPath相應屬性的初始值
toValue:keyPath相應屬性的結束值
 
3.隨着動畫的進行,在長度爲duration的持續時間內,keyPath相應屬性的值從fromValue漸漸地變爲toValue
 
4.若是fillMode=kCAFillModeForwards和removedOnComletion=NO,那麼在動畫執行完畢後,圖層會保持顯示動畫執行後的狀態。但在實質上,圖層的屬性值仍是動畫執行前的初始值,並無真正被改變。好比,CALayer的position初始值爲(0,0),CABasicAnimation的fromValue爲(10,10),toValue爲(100,100),雖然動畫執行完畢後圖層保持在(100,100)這個位置,實質上圖層的position仍是爲(0,0)
 
6、CAKeyframeAnimation
1.CApropertyAnimation的子類,跟CABasicAnimation的區別是:CABasicAnimation只能從一個數值(fromValue)變到另外一個數值(toValue),而CAKeyframeAnimation會使用一個NSArray保存這些數值
 
2.屬性解析:
values:就是上述的NSArray對象。裏面的元素稱爲」關鍵幀」(keyframe)。動畫對象會在指定的時間(duration)內,依次顯示values數組中的每個關鍵幀
path:能夠設置一個CGPathRef\CGMutablePathRef,讓層跟着路徑移動。path只對CALayer的anchorPoint和position起做用。若是你設置了path,那麼values將被忽
         略
keyTimes:能夠爲對應的關鍵幀指定對應的時間點,其取值範圍爲0到1.0,keyTimes中的每個時間值都對應values中的每一幀.當keyTimes沒有設置的時候,各個關鍵幀
                的時間是平分的
 
3.CABasicAnimation可看作是最多隻有2個關鍵幀的CAKeyframeAnimation
 
7、CAAnimationGroup
1.CAAnimation的子類,能夠保存一組動畫對象,將CAAnimationGroup對象加入層後,組中全部動畫對象能夠同時併發運行
 
2.屬性解析:
animations:用來保存一組動畫對象的NSArray
 
3.默認狀況下,一組動畫對象是同時運行的,也能夠經過設置動畫對象的beginTime屬性來更改動畫的開始時間
 
8、CATransition
1.CAAnimation的子類,用於作轉場動畫,可以爲層提供移出屏幕和移入屏幕的動畫效果。iOS比Mac OS X的轉場動畫效果少一點
 
2.UINavigationController就是經過CATransition實現了將控制器的視圖推入屏幕的動畫效果
 
3.屬性解析:
type:動畫過渡類型
subtype:動畫過渡方向
startProgress:動畫起點(在總體動畫的百分比)
endProgress:動畫終點(在總體動畫的百分比)
 
9、UIView動畫
1.UIKit直接將動畫集成到UIView類中,當內部的一些屬性發生改變時,UIView將爲這些改變提供動畫支持
 
2.執行動畫所須要的工做由UIView類自動完成,但仍要在但願執行動畫時通知視圖,爲此須要將改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間
 
3.常見方法解析:
+ (void)setAnimationDelegate:(id)delegate

設置動畫代理對象,當動畫開始或者結束時會發消息給代理對象緩存

+ (void)setAnimationWillStartSelector:(SEL)selector

當動畫即將開始時,執行delegate對象的selector,而且把beginAnimations:context:中傳入的參數傳進selector併發

+ (void)setAnimationDidStopSelector:(SEL)selector

當動畫結束時,執行delegate對象的selector,而且把beginAnimations:context:中傳入的參數傳進selector框架

+ (void)setAnimationDuration:(NSTimeInterval)duration

動畫的持續時間,秒爲單位函數

+ (void)setAnimationDelay:(NSTimeInterval)delay

動畫延遲delay秒後再開始性能

+ (void)setAnimationStartDate:(NSDate *)startDate

動畫的開始時間,默認爲now動畫

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve

動畫的節奏控制,具體看下面的」備註」atom

+ (void)setAnimationRepeatCount:(float)repeatCount

動畫的重複次數spa

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses

若是設置爲YES,表明動畫每次重複執行的效果會跟上一次相反線程

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache

設置視圖view的過渡效果, transition指定過渡類型, cache設置YES表明使用視圖緩存,性能較好

 

10、Block動畫

1.+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

參數解析:

duration:動畫的持續時間
delay:動畫延遲delay秒後開始
options:動畫的節奏控制
animations:將改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block
 
2.+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

參數解析:

duration:動畫的持續時間
view:須要進行轉場動畫的視圖
options:轉場動畫的類型
animations:將改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block
 
3.+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

方法調用完畢後,至關於執行了下面兩句代碼:

// 添加toView到父視圖

[fromView.superview addSubview:toView];

// 把fromView從父視圖中移除

[fromView.superview removeFromSuperview];

參數解析:

duration:動畫的持續時間
options:轉場動畫的類型
animations:將改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block

 

12、UIImageView的幀動畫

1.UIImageView可讓一系列的圖片在特定的時間內按順序顯示
 
2.相關屬性解析:
animationImages:要顯示的圖片(一個裝着UIImage的NSArray)
animationDuration:完整地顯示一次animationImages中的全部圖片所需的時間
animationRepeatCount:動畫的執行次數(默認爲0,表明無限循環)
 
3.相關方法解析:
- (void)startAnimating; 開始動畫
- (void)stopAnimating;  中止動畫
- (BOOL)isAnimating;  是否正在運行動畫
 
十3、UIActivityIndicatorView
1.是一個旋轉進度輪,能夠用來告知用戶有一個操做正在進行中,通常用initWithActivityIndicatorStyle初始化
 
2.方法解析:
- (void)startAnimating; 開始動畫
- (void)stopAnimating;  中止動畫
- (BOOL)isAnimating;  是否正在運行動畫
 
3.UIActivityIndicatorViewStyle有3個值可供選擇:
UIActivityIndicatorViewStyleWhiteLarge   //大型白色指示器    
UIActivityIndicatorViewStyleWhite      //標準尺寸白色指示器    
UIActivityIndicatorViewStyleGray    //灰色指示器,用於白色背景

 

十3、transform的屬性列表

 

代碼:

  1 //
  2 //  ViewController.m
  3 //  IOS_0223_核心動畫CoreAnimation
  4 //
  5 //  Created by ma c on 16/2/23.
  6 //  Copyright © 2016年 博文科技. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import <QuartzCore/QuartzCore.h>
 11 
 12 @interface ViewController ()
 13 @property (weak, nonatomic) IBOutlet UIImageView *imgView;
 14 
 15 @property (weak, nonatomic) IBOutlet UIView *greenView;
 16 
 17 @property (nonatomic, strong) CALayer *layer;
 18 
 19 @property (nonatomic, assign) BOOL isShake;
 20 
 21 @property (nonatomic, assign) int index;
 22 
 23 
 24 
 25 @end
 26 
 27 @implementation ViewController
 28 
 29 - (void)viewDidLoad {
 30     [super viewDidLoad];
 31     
 32     //[self createLayer];
 33     
 34 }
 35 ///建立圖層
 36 - (void)createLayer
 37 {
 38     CALayer *layer = [CALayer layer];
 39     layer.bounds = CGRectMake(0, 0, 100, 100);
 40     
 41     layer.position = CGPointMake(self.view.center.x-50, 200);
 42     layer.anchorPoint = CGPointZero;
 43     layer.backgroundColor = [UIColor redColor].CGColor;
 44     [self.view.layer addSublayer:layer];
 45     
 46     self.layer = layer;
 47 }
 48 
 49 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 50 {
 51 //    [self createTranslateAnimation];
 52 //    [self createScaleAnimation];
 53 //    [self createTransformAnimation];
 54 //    [self createMoveAnimation];
 55 //    [self createPathAnimation];
 56 //    [self iconShake];
 57 //    [self createTransitonAnimation];
 58 //    [self createGroupAnimation];
 59     [self createViewAnimation];
 60     
 61     
 62 }
 63 #pragma mark - UIView封裝的動畫
 64 - (void)createViewAnimation
 65 {
 66     // 1.
 67 //    [UIView beginAnimations:nil context:nil];
 68 //    self.greenView.center = CGPointMake(200, 200);
 69 //    //監聽動畫(動畫執行完畢後,自動執行animateStop的方法)
 70 //    [UIView setAnimationDelegate:self];
 71 //    [UIView setAnimationDidStopSelector:@selector(animateStop)];
 72 //    [UIView commitAnimations];
 73     
 74     // 2.
 75 //    [UIView animateWithDuration:1.0 animations:^{
 76 //        self.greenView.center = CGPointMake(200, 200);
 77 //    }completion:^(BOOL finished) {
 78 //        //動畫執行完畢後執行
 79 //    }];
 80     
 81     // 3.
 82     self.index++;
 83     if (self.index == 9) {
 84         self.index = 0;
 85     }
 86     NSString *fileName = [NSString stringWithFormat:@"%d.jpg",self.index];
 87     self.imgView.image = [UIImage imageNamed:fileName];
 88 
 89     [UIView transitionWithView:self.imgView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
 90         ;
 91     } completion:^(BOOL finished) {
 92         ;
 93     }];
 94 }
 95 
 96 #pragma mark - 動畫組
 97 - (void)createGroupAnimation
 98 {
 99     CAAnimationGroup *group = [CAAnimationGroup animation];
100     
101     // 1.建立縮放動畫對象
102     CABasicAnimation *scale = [CABasicAnimation animation];
103     scale.keyPath = @"transform.scale";
104     scale.toValue = @(0.0);
105     
106     // 2.建立旋轉動畫對象
107     CABasicAnimation *rotate = [CABasicAnimation animation];
108     rotate.keyPath = @"transform.rotation";
109     rotate.toValue = @(M_PI);
110     
111     group.animations = @[scale,rotate];
112     group.duration = 2.0;
113     
114     group.removedOnCompletion = NO;
115     group.fillMode = kCAFillModeForwards;
116     
117     // 3.添加動畫
118     [self.greenView.layer addAnimation:group forKey:nil];
119 
120     
121 }
122 
123 #pragma mark - 過渡動畫
124 - (void)createTransitonAnimation
125 {
126     self.index++;
127     if (self.index == 9) {
128         self.index = 0;
129     }
130     NSString *fileName = [NSString stringWithFormat:@"%d.jpg",self.index];
131     self.imgView.image = [UIImage imageNamed:fileName];
132     
133     //轉場動畫
134     CATransition *animation = [CATransition animation];
135     animation.type = @"cube";
136     animation.subtype = kCATransitionFromRight;
137     
138     animation.startProgress = 0.2;
139     animation.endProgress = 0.6;
140     
141     animation.duration = 0.5;
142     [self.imgView.layer addAnimation:animation forKey:nil];
143 }
144 
145 #pragma mark - 幀動畫
146 ///圖標抖動
147 - (void)iconShake
148 {
149     if (self.isShake) {
150         CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
151         animation.keyPath = @"transform.rotation";
152         
153         animation.values = @[@(-M_PI_4 / 4),@(M_PI_4 / 4),@(-M_PI_4 / 4)];
154         animation.duration = 0.25;
155         animation.repeatCount = MAXFLOAT;
156         
157         animation.removedOnCompletion = NO;
158         animation.fillMode = kCAFillModeForwards;
159         
160         [self.greenView.layer addAnimation:animation forKey:@"shake"];
161         
162         self.isShake = NO;
163     }
164     else
165     {
166         [self.greenView.layer removeAnimationForKey:@"shake"];
167         self.isShake = YES;
168     }
169 }
170 
171 - (void)createPathAnimation
172 {
173     CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
174     animation.keyPath = @"position";
175     
176     //設置動畫執行路徑
177     CGMutablePathRef path = CGPathCreateMutable();
178     CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
179     animation.path = path;
180     CGPathRelease(path);
181     
182     //設置動畫執行節奏
183     animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
184     
185     //設置動畫代理
186     animation.delegate = self;
187     
188     animation.duration = 3.0;
189     
190     animation.removedOnCompletion = NO;
191     animation.fillMode = kCAFillModeForwards;
192 
193     [self.greenView.layer addAnimation:animation forKey:nil];
194 }
195 
196 #pragma mark - 動畫代理
197 - (void)animationDidStart:(CAAnimation *)anim
198 {
199     NSLog(@"animationDidStart");
200 }
201 
202 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
203 {
204     NSLog(@"animationDidStop");
205 }
206 
207 - (void)createMoveAnimation
208 {
209     CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
210     animation.keyPath = @"position";
211     
212     NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(50, 200)];
213     NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
214     NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(200, 400)];
215     NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(50, 400)];
216     animation.values = @[v1,v2,v3,v4];
217     animation.duration = 3.0;
218     
219 //    animation.keyTimes = @[@(1),@(1.5),@(2)];
220 
221     animation.removedOnCompletion = NO;
222     animation.fillMode = kCAFillModeForwards;
223 
224     [self.greenView.layer addAnimation:animation forKey:nil];
225 }
226 
227 #pragma mark - 基礎動畫
228 /*只能在兩個值之間變化:fromValue -> toValue*/
229 
230 /// 動畫
231 - (void)createTransformAnimation
232 {
233     // 1.建立動畫對象
234     CABasicAnimation *animation = [CABasicAnimation animation];
235     
236     // 2.設置動畫對象
237 //    animation.keyPath = @"transform";
238 //    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)];
239     
240 //    animation.keyPath = @"transform.rotation";
241 //    animation.toValue = @(M_PI);
242     
243 //    animation.keyPath = @"transform.scale";
244 //    animation.toValue = @(1.5);
245     
246     animation.keyPath = @"transform.translation";
247     animation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
248     
249     animation.duration = 3.0;
250     
251     animation.removedOnCompletion = NO;
252     animation.fillMode = kCAFillModeForwards;
253     
254     // 3.添加動畫
255     [self.layer addAnimation:animation forKey:nil];
256 
257 }
258 
259 ///縮放動畫
260 - (void)createScaleAnimation
261 {
262     // 1.建立動畫對象
263     CABasicAnimation *animation = [CABasicAnimation animation];
264     
265     // 2.設置動畫對象
266     animation.keyPath = @"bounds";
267     animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
268     animation.duration = 3.0;
269     
270     animation.removedOnCompletion = NO;
271     animation.fillMode = kCAFillModeForwards;
272     
273     // 3.添加動畫
274     [self.layer addAnimation:animation forKey:nil];
275 
276     
277 }
278 ///平移動畫
279 - (void)createTranslateAnimation
280 {
281     // 1.建立動畫對象
282     CABasicAnimation *animation = [CABasicAnimation animation];
283     
284     // 2.設置動畫對象
285     //keyPath決定了執行哪一個動畫,調整哪一個layer屬性來執行動畫
286     animation.keyPath = @"position";
287 //    animation.fromValue =[NSValue valueWithCGPoint:CGPointZero];
288     //toValue:變成什麼值
289 //    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
290     //byValue:增長多少值
291     animation.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
292     animation.duration = 3.0;
293     
294     //設置動畫結束時,不要刪除動畫
295     animation.removedOnCompletion = NO;
296     //保持最新狀態
297     animation.fillMode = kCAFillModeForwards;
298     
299     // 3.添加動畫
300     [self.layer addAnimation:animation forKey:nil];
301 
302 }
303 @end
相關文章
相關標籤/搜索