iOS開發UI篇—核心動畫(UIView封裝動畫)

1、UIView動畫(首尾)緩存

1.簡單說明app

UIKit直接將動畫集成到UIView類中,當內部的一些屬性發生改變時,UIView將爲這些改變提供動畫支持iview

執行動畫所須要的工做由UIView類自動完成,但仍要在但願執行動畫時通知視圖,爲此須要將改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間性能

常見方法解析:動畫

+ (void)setAnimationDelegate:(id)delegate     設置動畫代理對象,當動畫開始或者結束時會發消息給代理對象ui

+ (void)setAnimationWillStartSelector:(SEL)selector   當動畫即將開始時,執行delegate對象的selector,而且把beginAnimations:context:中傳入的參數傳進selectoratom

+ (void)setAnimationDidStopSelector:(SEL)selector  當動畫結束時,執行delegate對象的selector,而且把beginAnimations:context:中傳入的參數傳進selectorspa

+ (void)setAnimationDuration:(NSTimeInterval)duration   動畫的持續時間,秒爲單位代理

+ (void)setAnimationDelay:(NSTimeInterval)delay  動畫延遲delay秒後再開始code

+ (void)setAnimationStartDate:(NSDate *)startDate   動畫的開始時間,默認爲now

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve  動畫的節奏控制

+ (void)setAnimationRepeatCount:(float)repeatCount  動畫的重複次數

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses  若是設置爲YES,表明動畫每次重複執行的效果會跟上一次相反

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache  設置視圖view的過渡效果, transition指定過渡類型, cache設置YES表明使用視圖緩存,性能較好

 

2.代碼示例

複製代碼

 1 // 2 //  YYViewController.m 3 //  01-uiview封裝動畫 4 // 5 //  Created by apple on 14-6-22. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 11 @interface YYViewController ()12 @property (weak, nonatomic) IBOutlet UIView *customView;13 14 15 @end16 17 @implementation YYViewController18 19 - (void)viewDidLoad20 {21     [super viewDidLoad];22     23 }24 25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event26 {27     //打印動畫塊的位置28     NSLog(@"動畫執行以前的位置:%@",NSStringFromCGPoint(self.customView.center));29     30     //首尾式動畫31     [UIView beginAnimations:nil context:nil];32     //執行動畫33     //設置動畫執行時間34     [UIView setAnimationDuration:2.0];35     //設置代理36     [UIView setAnimationDelegate:self];37     //設置動畫執行完畢調用的事件38     [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];39     self.customView.center=CGPointMake(200, 300);40     [UIView commitAnimations];41 42 }43 44 -(void)didStopAnimation45 {46     NSLog(@"動畫執行完畢");47     //打印動畫塊的位置48     NSLog(@"動畫執行以後的位置:%@",NSStringFromCGPoint(self.customView.center));49 }50 @end

複製代碼

執行結果:

    

打印動畫塊的位置:

3.UIView封裝的動畫與CALayer動畫的對比

使用UIView和CALayer都能實現動畫效果,可是在真實的開發中,通常仍是主要使用UIView封裝的動畫,而不多使用CALayer的動畫。

CALayer核心動畫與UIView動畫的區別:
UIView封裝的動畫執行完畢以後不會反彈。即若是是經過CALayer核心動畫改變layer的位置狀態,表面上看雖然已經改變了,可是實際上它的位置是沒有改變的。

代碼示例:

複製代碼

 1 // 2 //  YYViewController.m 3 //  01-uiview封裝動畫 4 // 5 //  Created by apple on 14-6-22. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 11 @interface YYViewController ()12 @property (weak, nonatomic) IBOutlet UIView *customView;13 14 15 @end16 17 @implementation YYViewController18 19 20 21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event22 {23    //1.建立核心動畫24     CABasicAnimation *anima=[CABasicAnimation animation];25     //平移26     anima.keyPath=@"position";27     //設置執行的動畫28     anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];29     30     //設置執行動畫的時間31     anima.duration=2.0;32     //設置動畫執行完畢以後不刪除動畫33     anima.removedOnCompletion=NO;34     //設置保存動畫的最新狀態35     anima.fillMode=kCAFillModeForwards;36 //    anima.fillMode=kCAFillModeBackwards;37     38     //設置動畫的代理39     anima.delegate=self;40     41     //2.添加核心動畫42     [self.customView.layer addAnimation:anima forKey:nil];43 }44 45 -(void)animationDidStart:(CAAnimation *)anim46 {47     //打印動畫塊的位置48 //    NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint(self.customView.center));49     NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint( self.customView.layer.position));50 }51 -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag52 {53     //打印動畫塊的位置54     NSLog(@"動畫執行完畢後的位置:%@",NSStringFromCGPoint( self.customView.layer.position));55 }56 57 @end

複製代碼

打印結果:

 

2、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

轉場動畫

+ (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

 

+ (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

 

2.代碼示例

複製代碼

 1 #import "YYViewController.h" 2  3 @interface YYViewController () 4 @property (weak, nonatomic) IBOutlet UIView *customView; 5  6 @end 7  8 @implementation YYViewController 9 10 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event11 {12     //block代碼塊動畫13         [UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{14             //執行的動畫15             NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint(self.customView.center));16             self.customView.center=CGPointMake(200, 300);17         } completion:^(BOOL finished) {18             //動畫執行完畢後的首位操做19             NSLog(@"動畫執行完畢");20             NSLog(@"動畫執行完畢後的位置:%@",NSStringFromCGPoint( self.customView.center));21         }];22 }23 @end

複製代碼

打印結果:

提示:self.customView.layer.position和self.customView.center等價,由於position的默認值爲(0.5,0.5)。

 

3、補充

1.UIImageView的幀動畫

UIImageView可讓一系列的圖片在特定的時間內按順序顯示

相關屬性解析:

animationImages:要顯示的圖片(一個裝着UIImage的NSArray)

animationDuration:完整地顯示一次animationImages中的全部圖片所需的時間

animationRepeatCount:動畫的執行次數(默認爲0,表明無限循環)

相關方法解析:

- (void)startAnimating; 開始動畫

- (void)stopAnimating;  中止動畫

- (BOOL)isAnimating;  是否正在運行動畫

 

2.UIActivityIndicatorView

是一個旋轉進度輪,能夠用來告知用戶有一個操做正在進行中,通常用initWithActivityIndicatorStyle初始化

方法解析:

- (void)startAnimating; 開始動畫

- (void)stopAnimating;  中止動畫

- (BOOL)isAnimating;  是否正在運行動畫

UIActivityIndicatorViewStyle有3個值可供選擇:

UIActivityIndicatorViewStyleWhiteLarge   //大型白色指示器    

UIActivityIndicatorViewStyleWhite      //標準尺寸白色指示器    

UIActivityIndicatorViewStyleGray    //灰色指示器,用於白色背景

相關文章
相關標籤/搜索