日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS动画开发总结

發布時間:2025/4/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS动画开发总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iOS開發動畫(Animation)總結

http://blog.csdn.net/mad2man/article/details/17554621


UIView的,翻轉、旋轉,偏移,翻頁,縮放,取反的動畫效果


翻轉的動畫
//開始動畫 [UIView beginAnimations:@"doflip" context:nil]; //設置時常 [UIView setAnimationDuration:1]; //設置動畫淡入淡出 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //設置代理 [UIView setAnimationDelegate:self]; //設置翻轉方向 [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:manImageView cache:YES]; //動畫結束 [UIView commitAnimations];
旋轉動畫
//創建一個CGAffineTransform transform對象 CGAffineTransform transform; //設置旋轉度數 transform = CGAffineTransformRotate(manImageView.transform,M_PI/6.0); //動畫開始 [UIView beginAnimations:@"rotate" context:nil ]; //動畫時常 [UIView setAnimationDuration:2]; //添加代理 [UIView setAnimationDelegate:self]; //獲取transform的值 [manImageView setTransform:transform]; //關閉動畫 [UIView commitAnimations];
偏移動畫
[UIView beginAnimations:@"move" context:nil];[UIView setAnimationDuration:2];[UIView setAnimationDelegate:self];//改變它的frame的x,y的值manImageView.frame=CGRectMake(100,100, 120,100);[UIView commitAnimations];

翻頁動畫
[UIView beginAnimations:@"curlUp" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//指定動畫曲線類型,該枚舉是默認的,線性的是勻速的 //設置動畫時常 [UIView setAnimationDuration:1]; [UIView setAnimationDelegate:self]; //設置翻頁的方向 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:manImageView cache:YES]; //關閉動畫 [UIView commitAnimations];

縮放動畫
CGAffineTransform transform; transform = CGAffineTransformScale(manImageView.transform,1.2,1.2); [UIView beginAnimations:@"scale" context:nil]; [UIView setAnimationDuration:2]; [UIView setAnimationDelegate:self]; [manImageView setTransform:transform]; [UIView commitAnimations];

取反的動畫效果是根據當前的動畫取他的相反的動畫

CGAffineTransform transform; transform=CGAffineTransformInvert(manImageView.transform); [UIView beginAnimations:@"Invert" context:nil]; [UIView setAnimationDuration:2];//動畫時常 [UIView setAnimationDelegate:self]; [manImageView setTransform:transform];//獲取改變后的view的transform [UIView commitAnimations];//關閉動畫
========

IOS開發-UIView之動畫效果的實現方法(合集)

http://www.tuicool.com/articles/BjMrQne
原文 ?http://www.cnblogs.com/GarveyCalvin/p/4193963.html
主題 UIView
前言:在開發APP中,我們會經常使用到動畫效果。使用動畫可以讓我們的APP更酷更炫,最重要的是優化用戶體驗,但取決于動畫的質量。像QQ、微信、新浪微博等APP,動畫效果就很好了,至少我很喜歡它們的動畫,讓我使用起來感覺很順暢,心情很開朗。本文會介紹UIView效果的實現方法,非核心動畫。


一、使用UIView類實現動畫

基本寫法,代碼必須放在Begin和Commit之間:

[UIView beginAnimations:nil context:nil]; // 開始動畫
// Code...
[UIView commitAnimations]; // 提交動畫
簡單例子:

[UIView beginAnimations:nil context:nil]; // 開始動畫
[UIView setAnimationDuration:10.0]; // 動畫時長

/**
?* ?圖像向下移動
?*/
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];


[UIView commitAnimations]; // 提交動畫
同時運行多個動畫效果:


[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[_imageView setAlpha:0.0];
[UIView commitAnimations];


[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
[UIView commitAnimations];
以上代碼實現的動畫效果為( 同時執行 ):


1、圖像向下平移150像像


2、設置圖像透明度為0。


指定上下文:


CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:2.0];
[_imageView setAlpha:0];
[UIView commitAnimations];
UIGraphicsGetCurrentContext():獲取當前視圖的上下文


其它方法及屬性:

以下方法及屬性不為全部,只例舉部分(其它沒提及到的方法及屬性請自行嘗試,謝謝):

// 開始動畫
+ (void)beginAnimations:(NSString *)animationID context:(void *)context;

// 提交動畫
+ (void)commitAnimations;?

// 設置動畫曲線,默認是勻速進行:
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;


// 設置動畫時長:
+ (void)setAnimationDuration:(NSTimeInterval)duration;
?
// 默認為YES。為NO時跳過動畫效果,直接跳到執行后的狀態。
+ (void)setAnimationsEnabled:(BOOL)enabled;

// 設置動畫延遲執行(delay:秒為單位):
+ (void)setAnimationDelay:(NSTimeInterval)delay;
?
// 動畫的重復播放次數
+ (void)setAnimationRepeatCount:(float)repeatCount;

// 如果為YES,逆向(相反)動畫效果,結束后返回動畫逆向前的狀態; 默認為NO:
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;


// 設置動畫代理:
+ (void)setAnimationDelegate:(id)delegate;
?
// 動畫將要開始時執行方法××(必須要先設置動畫代理):
+ (void)setAnimationWillStartSelector:(SEL)selector;

// 動畫已結束時執行方法××(必須要先設置動畫代理):
+ (void)setAnimationDidStopSelector:(SEL)selector;

/**
?* ?設置動畫過渡效果
?*
?* ?@param transition 動畫的過渡效果
?* ?@param view 過渡效果作用視圖
?* ?@param cache 如果為YES,開始和結束視圖分別渲染一次并在動畫中創建幀;否則,視圖將會渲染每一幀。例如,你不需要在視圖轉變中不停的更新,你只需要等到轉換完成再去更新視圖。
?*/
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

// 刪除所有動畫層
// 注意:層指的是layout,例:[_imageView.layer removeAllAnimations];
- (void)removeAllAnimations;
二、使用UIView的動畫塊代碼:

方法一:

[UIView animateWithDuration:4.0 // 動畫時長
? ? ? ? ? ? ? ? ?animations:^{
? ? ? ? ? ? ? ? ? ? ?// code
? ? ? ? ? ? ? ? ?}];
方法二:

[UIView animateWithDuration:4.0 // 動畫時長
? ? ? ? ?animations:^{
? ? ? ? ? ?// code...
? ? ? ? ?}
? ? ? ? ?completion:^(BOOL finished) {
? ? ? ? ? ?// 動畫完成后執行
? ? ? ? ? ?// code...
? ? ? ? ?}];
方法三:

[UIView animateWithDuration:4.0 // 動畫時長
? ? ? ? ? ? delay:2.0 // 動畫延遲
? ? ? ? ? options:UIViewAnimationOptionCurveEaseIn // 動畫過渡效果
? ? ? ? ?animations:^{
? ? ? ? ? ?// code...
? ? ? ? ?}
? ? ? ? ?completion:^(BOOL finished) {
? ? ? ? ? ?// 動畫完成后執行
? ? ? ? ? ?// code...
? ? ? ? ?}];
方法四,Spring Animationring Animation):

在IOS7開始,系統動畫效果廣泛應用Spring Animation :

[UIView animateWithDuration:4.0 // 動畫時長
? ? ? ? ? ? delay:0.0 // 動畫延遲
? ?usingSpringWithDamping:1.0 // 類似彈簧振動效果 0~1
? ? initialSpringVelocity:5.0 // 初始速度
? ? ? ? ? options:UIViewAnimationOptionCurveEaseInOut // 動畫過渡效果
? ? ? ? ?animations:^{
? ? ? ? ? ?// code...
? ? ? ? ? ?CGPoint point = _imageView.center;
? ? ? ? ? ?point.y += 150;
? ? ? ? ? ?[_imageView setCenter:point];
? ? ? ? ?} completion:^(BOOL finished) {
? ? ? ? ? ?// 動畫完成后執行
? ? ? ? ? ?// code...
? ? ? ? ? ?[_imageView setAlpha:1];
? ? ? ? ?}];
usingSpringWithDamping:它的范圍為 0.0f 到 1.0f ,數值越小「彈簧」的振動效果越明顯。

initialSpringVelocity:初始的速度,數值越大一開始移動越快。值得注意的是,初始速度取值較高而時間較短時,也會出現反彈情況。

轉:Spring Animation 是線性動畫或 ease-out 動畫的理想替代品。由于 iOS 本身大量使用的就是 Spring Animation,用戶已經習慣了這種動畫效果,因此使用它能使 App 讓人感覺更加自然,用 Apple 的話說就是「instantly familiar」。此外,Spring Animation 不只能對位置使用,它適用于所有可被添加動畫效果的屬性。
方法五,關鍵幀動畫:

UIView動畫已經具備高級的方法來創建動畫,而且可以更好地理解和構建動畫。IOS7以后蘋果新加了一個animateKeyframesWithDuration的方法,我們可以使用它來創建更多更復雜更酷炫的動畫效果,而不需要去使用到核心動畫(CoreAnimatino)。
創建關鍵幀方法:

/**
?* ?添加關鍵幀方法
?*
?* ?@param duration ? 動畫時長
?* ?@param delay ? ? ?動畫延遲
?* ?@param options ? ?動畫效果選項
?* ?@param animations 動畫執行代碼
?* ?@param completion 動畫結束執行代碼
?*/
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?delay:(NSTimeInterval)delay
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?options:(UIViewKeyframeAnimationOptions)options
? ? ? ? ? ? ? ? ? ? ? ? ? animations:(void (^)(void))animations
? ? ? ? ? ? ? ? ? ? ? ? ? completion:(void (^)(BOOL finished))completion;
添加關鍵幀方法:

/**
?* ?添加關鍵幀
?*
?* ?@param frameStartTime 動畫相對開始時間
?* ?@param frameDuration ?動畫相對持續時間
?* ?@param animations ? ? 動畫執行代碼
?*/
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
? ? ? ? ? ? ? ? ? ? ? ? relativeDuration:(double)frameDuration
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? animations:(void (^)(void))animations;
以上說的相對時間,也就是說:“它們自身會根據動畫總持續時長自動匹配其運行時長”。

下面用一個簡單的示例作解答,彩虹變化視圖:

void (^keyFrameBlock)() = ^(){
? ? // 創建顏色數組
? ? NSArray *arrayColors = @[[UIColor orangeColor],
? ? ? [UIColor yellowColor],
? ? ? [UIColor greenColor],
? ? ? [UIColor blueColor],
? ? ? [UIColor purpleColor],
? ? ? [UIColor redColor]];
? ? NSUInteger colorCount = [arrayColors count];
? ? // 循環添加關鍵幀
? ? for (NSUInteger i = 0; i < colorCount; i++) {
? ? ? ? [UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
? ? ? ? ?relativeDuration:1 / (CGFloat)colorCount
? ? ? ? ? ? ? ?animations:^{
? ? ? ? ? ? ? ? ? ?[_graduallyView setBackgroundColor:arrayColors[i]];
? ? ? ? ? ? ? ?}];
? ? }
};
[UIView animateKeyframesWithDuration:4.0
? ? ? ? delay:0.0
? ? ? options:UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear
? ?animations:keyFrameBlock
? ?completion:^(BOOL finished) {
? ? ? ?// 動畫完成后執行
? ? ? ?// code...
? ?}];
動畫過渡效果(Options),新增了以下幾個:

UIViewKeyframeAnimationOptionCalculationModeLinear ? ? = 0 << 10, // default
UIViewKeyframeAnimationOptionCalculationModeDiscrete ? = 1 << 10,
UIViewKeyframeAnimationOptionCalculationModePaced ? ? ?= 2 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubic ? ? ?= 3 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10
下面我們看一張圖,讓我們更容易理解:


小結:

UIView實現動畫的方法有很多種。簡單的動畫效果你可以隨意丟,比較復雜的動畫效果你可以選用關鍵幀KeyFrame方法。

至于選用哪種,就需要根據產品需求去進行判斷。

本文參考文章:

http://www.tuicool.com/articles/FjiQJbF

http://www.tuicool.com/articles/ZR7nYv
========

iOS開發UI篇—核心動畫(基礎動畫)

http://www.cnblogs.com/wendingding/p/3801157.html


一、簡單介紹


CAPropertyAnimation的子類


屬性解析:

fromValue:keyPath相應屬性的初始值

toValue:keyPath相應屬性的結束值

隨著動畫的進行,在長度為duration的持續時間內,keyPath相應屬性的值從fromValue漸漸地變為toValue

如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動畫執行完畢后,圖層會保持顯示動畫執行后的狀態。但在實質上,圖層的屬性值還是動畫執行前的初始值,并沒有真正被改變。

比如,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10),toValue為(100,100),雖然動畫執行完畢后圖層保持在(100,100)這個位置,實質上圖層的position還是為(0,0)

?
二、平移動畫

代碼示例:


?//
?// ?YYViewController.m
?// ?07-核心動畫(基礎動畫)
?//
?// ?Created by apple on 14-6-21.
?// ?Copyright (c) 2014年 itcase. All rights reserved.
?//
?
?#import "YYViewController.h"

@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end

@implementation YYViewController

- (void)viewDidLoad
{
? ? [super viewDidLoad];
? ??
? ? //創建layer
? ? CALayer *myLayer=[CALayer layer];
? ? //設置layer的屬性
? ? myLayer.bounds=CGRectMake(0, 0, 50, 80);
? ? myLayer.backgroundColor=[UIColor yellowColor].CGColor;
? ? myLayer.position=CGPointMake(50, 50);
? ? myLayer.anchorPoint=CGPointMake(0, 0);
? ? myLayer.cornerRadius=20;
? ? //添加layer
? ? [self.view.layer addSublayer:myLayer];
? ? self.myLayer=myLayer;
}

//設置動畫(基礎動畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
? ? //1.創建核心動畫
? ? // ? ?CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
? ? CABasicAnimation *anima=[CABasicAnimation animation];
? ??
? ? //1.1告訴系統要執行什么樣的動畫
? ? anima.keyPath=@"position";
? ? //設置通過動畫,將layer從哪兒移動到哪兒
? ? anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
? ? anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
? ??
? ? //1.2設置動畫執行完畢之后不刪除動畫
? ? anima.removedOnCompletion=NO;
? ? //1.3設置保存動畫的最新狀態
? ? anima.fillMode=kCAFillModeForwards;

? ? //2.添加核心動畫到layer
? ?[self.myLayer addAnimation:anima forKey:nil];

}
  @end

代碼說明:

?第42行設置的keyPath是@"position",說明要修改的是CALayer的position屬性,也就是會執行平移動畫

?第44,45行,這里的屬性接收的時id類型的參數,因此并不能直接使用CGPoint這種結構體類型,而是要先包裝成NSValue對象后再使用。

?默認情況下,動畫執行完畢后,動畫會自動從CALayer上移除,CALayer又會回到原來的狀態。為了保持動畫執行后的狀態,可以加入第48,50行代碼

byValue和toValue的區別,前者是在當前的位置上增加多少,后者是到指定的位置。
?
執行效果:

設置代理:設置動畫的代理,可以監聽動畫的執行過程,這里設置控制器為代理。

代碼示例:

#import "YYViewController.h"

@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end

@implementation YYViewController

- (void)viewDidLoad
{
? ? [super viewDidLoad];
? ?
? ? //創建layer
? ? CALayer *myLayer=[CALayer layer];
? ? //設置layer的屬性
? ? myLayer.bounds=CGRectMake(0, 0, 50, 80);
? ? myLayer.backgroundColor=[UIColor yellowColor].CGColor;
? ? myLayer.position=CGPointMake(50, 50);
? ? myLayer.anchorPoint=CGPointMake(0, 0);
? ? myLayer.cornerRadius=20;
? ? //添加layer
? ? [self.view.layer addSublayer:myLayer];
? ? self.myLayer=myLayer;
}

//設置動畫(基礎動畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
? ? //1.創建核心動畫
? ? // ? ?CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
? ? CABasicAnimation *anima=[CABasicAnimation animation];
? ??
? ? //1.1告訴系統要執行什么樣的動畫
? ? anima.keyPath=@"position";
? ? //設置通過動畫,將layer從哪兒移動到哪兒
? ?anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
? ? anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
? ??
? ? //1.2設置動畫執行完畢之后不刪除動畫
? ? anima.removedOnCompletion=NO;
? ? //1.3設置保存動畫的最新狀態
? ? anima.fillMode=kCAFillModeForwards;
? ? anima.delegate=self;
? ? //打印
? ? NSString *str=NSStringFromCGPoint(self.myLayer.position);
? ? NSLog(@"執行前:%@",str);
? ??
? ? //2.添加核心動畫到layer
? ? [self.myLayer addAnimation:anima forKey:nil];

}

-(void)animationDidStart:(CAAnimation *)anim
{
? ? NSLog(@"開始執行動畫");
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
? ? //動畫執行完畢,打印執行完畢后的position值
? ? NSString *str=NSStringFromCGPoint(self.myLayer.position);
? ? NSLog(@"執行后:%@",str);
}

@end

打印position的屬性值,驗證圖層的屬性值還是動畫執行前的初始值{50,50},并沒有真正被改變為{200,300}。


三、縮放動畫


實現縮放動畫的代碼示例:


復制代碼
?1 //
?2 // ?YYViewController.m
?3 // ?08-核心動畫平移
?4 //
?5 // ?Created by apple on 14-6-21.
?6 // ?Copyright (c) 2014年 itcase. All rights reserved.
?7 //
?8?
?9 #import "YYViewController.h"
10?
11 @interface YYViewController ()
12 @property(nonatomic,strong)CALayer *myLayer;
13 @end
14?
15 @implementation YYViewController
16?
17 - (void)viewDidLoad
18 {
19 ? ? [super viewDidLoad];
20 ? ??
21 ? ? //創建layer
22 ? ? CALayer *myLayer=[CALayer layer];
23 ? ? //設置layer的屬性
24 ? ? myLayer.bounds=CGRectMake(0, 0, 150, 60);
25 ? ? myLayer.backgroundColor=[UIColor yellowColor].CGColor;
26 ? ? myLayer.position=CGPointMake(50, 50);
27 ? ? myLayer.anchorPoint=CGPointMake(0, 0);
28 ? ? myLayer.cornerRadius=40;
29 ? ? //添加layer
30 ? ? [self.view.layer addSublayer:myLayer];
31 ? ? self.myLayer=myLayer;
32 }
33?
34 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
35 {
36 ? ? //1.創建動畫
37 ? ? CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
38 ? ? //1.1設置動畫執行時間
39 ? ? anima.duration=2.0;
40 ? ? //1.2設置動畫執行完畢后不刪除動畫
41 ? ? anima.removedOnCompletion=NO;
42 ? ? //1.3設置保存動畫的最新狀態
43 ? ? anima.fillMode=kCAFillModeForwards;
44 ? ? //1.4修改屬性,執行動畫
45 ? ? anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
46 ? ? //2.添加動畫到layer
47 ? ? [self.myLayer addAnimation:anima forKey:nil];
48 }
49?
50 @end
復制代碼
實現效果:


??


四、旋轉動畫


代碼示例:


復制代碼
?1 //
?2 // ?YYViewController.m
?3 // ?09-核心動畫旋轉
?4 //
?5 // ?Created by apple on 14-6-21.
?6 // ?Copyright (c) 2014年 itcase. All rights reserved.
?7 //
?8?
?9 #import "YYViewController.h"
10?
11 @interface YYViewController ()
12 @property(nonatomic,strong)CALayer *myLayer;
13 @end
14?
15 @implementation YYViewController
16 - (void)viewDidLoad
17 {
18 ? ? [super viewDidLoad];
19 ? ??
20 ? ? //創建layer
21 ? ? CALayer *myLayer=[CALayer layer];
22 ? ? //設置layer的屬性
23 ? ? myLayer.bounds=CGRectMake(0, 0, 150, 60);
24 ? ? myLayer.backgroundColor=[UIColor yellowColor].CGColor;
25 ? ? myLayer.position=CGPointMake(50, 50);
26 ? ? myLayer.anchorPoint=CGPointMake(0, 0);
27 ? ? myLayer.cornerRadius=40;
28 ? ? //添加layer
29 ? ? [self.view.layer addSublayer:myLayer];
30 ? ? self.myLayer=myLayer;
31 }
32?
33 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
34 {
35 ? ? //1.創建動畫
36 ? ? CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
37 ? ? //1.1設置動畫執行時間
38 ? ? anima.duration=2.0;
39 ? ? //1.2修改屬性,執行動畫
40 ? ? anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
41 ? ? //1.3設置動畫執行完畢后不刪除動畫
42 ? ? anima.removedOnCompletion=NO;
43 ? ? //1.4設置保存動畫的最新狀態
44 ? ? anima.fillMode=kCAFillModeForwards;
45 ? ??
46 ? ? //2.添加動畫到layer
47 ? ? [self.myLayer addAnimation:anima forKey:nil];
48 }
49 @end
復制代碼
實現效果:
? ?


提示:如果要讓圖形以2D的方式旋轉,只需要把CATransform3DMakeRotation在z方向上的值改為1即可。


anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];


四、補充


可以通過transform(KVC)的方式來進行設置。


代碼示例(平移):


復制代碼
?1 #import "YYViewController.h"
?2?
?3 @interface YYViewController ()
?4 @property(nonatomic,strong)CALayer *myLayer;
?5 @end
?6?
?7 @implementation YYViewController
?8 - (void)viewDidLoad
?9 {
10 ? ? [super viewDidLoad];
11 ? ??
12 ? ? //創建layer
13 ? ? CALayer *myLayer=[CALayer layer];
14 ? ? //設置layer的屬性
15 ? ? myLayer.bounds=CGRectMake(0, 0, 150, 60);
16 ? ? myLayer.backgroundColor=[UIColor yellowColor].CGColor;
17 ? ? myLayer.position=CGPointMake(50, 50);
18 ? ? myLayer.anchorPoint=CGPointMake(0, 0);
19 ? ? myLayer.cornerRadius=40;
20 ? ? //添加layer
21 ? ? [self.view.layer addSublayer:myLayer];
22 ? ? self.myLayer=myLayer;
23 }
24?
25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
26 {
27 ? ? //1.創建動畫
28 ? ? CABasicAnimation *anima=[CABasicAnimation animation];
29 ? ? anima.keyPath=@"transform";
30 ? ? //1.1設置動畫執行時間
31 ? ? anima.duration=2.0;
32 ? ? //1.2修改屬性,執行動畫
33 ??
34 ? ? anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)];
35 ? ? //1.3設置動畫執行完畢后不刪除動畫
36 ? ? anima.removedOnCompletion=NO;
37 ? ? //1.4設置保存動畫的最新狀態
38 ? ? anima.fillMode=kCAFillModeForwards;
39 ? ??
40 ? ? //2.添加動畫到layer
41 ? ? [self.myLayer addAnimation:anima forKey:nil];
42 }
復制代碼
實現效果:


繪制的圖形在y的方向上移動100個單位。
========

iOS開發UI篇—核心動畫簡介

http://www.cnblogs.com/wendingding/p/3801036.html


一、簡單介紹


Core Animation,中文翻譯為核心動畫,它是一組非常強大的動畫處理API,使用它能做出非常炫麗的動畫效果,而且往往是事半功倍。也就是說,使用少量的代碼就可以實現非常強大的功能。


Core Animation是跨平臺的,可以用在Mac OS X和iOS平臺。


Core Animation的動畫執行過程都是在后臺操作的,不會阻塞主線程。不阻塞主線程,可以理解為在執行動畫的時候還能點擊(按鈕)。


要注意的是,Core Animation是直接作用在CALayer上的,并非UIView。
?
二、Core Animation的使用步驟


1.使用它需要先添加QuartzCore.framework框架和引入主頭文件<QuartzCore/QuartzCore.h>(iOS7不需要)


2.初始化一個CAAnimation對象,并設置一些動畫相關屬性


3.通過調用CALayer的addAnimation:forKey:方法增加CAAnimation對象到CALayer中,這樣就能開始執行動畫了


4.通過調用CALayer的removeAnimationForKey:方法可以停止CALayer中的動畫




三、CAAnimation


類的繼承結構圖
 


CAAnimation是所有動畫類的父類,但是它不能直接使用,應該使用它的子類。


?常見屬性有:


duration:動畫的持續時間


repeatCount:動畫的重復次數


timingFunction:控制動畫運行的節奏


說明:(1)能用的動畫類只有4個子類:CABasicAnimation、CAKeyframeAnimation、CATransition、CAAnimationGroup


   ? (2)CAMediaTiming是一個協議(protocol)。


CAPropertyAnimation是CAAnimation的子類,但是不能直接使用,要想創建動畫對象,應該使用它的兩個子類:CABasicAnimation和CAKeyframeAnimation


它有個NSString類型的keyPath屬性,你可以指定CALayer的某個屬性名為keyPath,并且對CALayer的這個屬性的值進行修改,達到相應的動畫效果。比如,指定@"position"為keyPath,就會修改CALayer的position屬性的值,以達到平移的動畫效果




四、補充說明


所有動畫對象的父類,負責控制動畫的持續時間和速度,是個抽象類,不能直接使用,應該使用它具體的子類


屬性解析:(紅色代表來自CAMediaTiming協議的屬性)


duration:動畫的持續時間


repeatCount:動畫的重復次數


repeatDuration:動畫的重復時間


removedOnCompletion:默認為YES,代表動畫執行完畢后就從圖層上移除,圖形會恢復到動畫執行前的狀態。如果想讓圖層保持顯示動畫執行后的狀態,那就設置為NO,不過還要設置fillMode為kCAFillModeForwards


fillMode:決定當前對象在非active時間段的行為.比如動畫開始之前,動畫結束之后


beginTime:可以用來設置動畫延遲執行時間,若想延遲2s,就設置為CACurrentMediaTime()+2,CACurrentMediaTime()為圖層的當前時間


timingFunction:速度控制函數,控制動畫運行的節奏


delegate:動畫代理
========

總結

以上是生活随笔為你收集整理的IOS动画开发总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。