自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)UIView的Animation效果

移動(dòng)開發(fā) iOS
現(xiàn)實(shí)比想象的美好, 蘋果公司為開發(fā)者思考了一些問題,通過使用UIKit提供的動(dòng)畫支持,開發(fā)者只需要簡(jiǎn)單的幾行代碼就能實(shí)現(xiàn)各種各樣的動(dòng)畫效果。在UIKit中,所有的動(dòng)畫效果支持的方法都在UIView類中。

所謂動(dòng)畫效果,就是會(huì)動(dòng)的畫,到iOS App中來說的話,就是各種UIView的移動(dòng)。 想想看,如果我們自己來實(shí)現(xiàn)所有UIView的動(dòng)畫效果,需要考慮些什么東西呢?

* 該UIView現(xiàn)在在哪兒?

* 該UIView最后會(huì)動(dòng)到哪兒?

* 該UIView以什么樣的方式移動(dòng)到那兒?

* 該動(dòng)畫持續(xù)多長(zhǎng)時(shí)間?

* 每次移動(dòng)的最小時(shí)間間隔?

* 每次最小時(shí)間間隔的移動(dòng)的應(yīng)該移動(dòng)到哪兒?

* ….

想想這是一個(gè)多么殺腦細(xì)胞的過程,尤其是每一次的動(dòng)畫過程都要重復(fù)這一折磨的過程。

還好,現(xiàn)實(shí)比想象的美好, 蘋果公司為開發(fā)者思考了上面的問題,通過使用UIKit提供的動(dòng)畫支持,開發(fā)者只需要簡(jiǎn)單的幾行代碼就能實(shí)現(xiàn)各種各樣的動(dòng)畫效果。在UIKit中,所有的動(dòng)畫效果支持的方法都在UIView類中。

首先,在UIView中有很多屬性用以描述一個(gè)UIView的狀態(tài),而動(dòng)畫就是讓UIView從一個(gè)狀態(tài)平滑的過渡到另外一個(gè)狀態(tài)的過程。這些屬性有:

屬性名

作用

frame

控制UIView的大小和該UIView在superview中的相對(duì)位置。

bounds

控制UIView的大小

center

控制UIView的位置

transform

控制UIView的縮放,旋轉(zhuǎn)角度等固定好中心位置之后的變化

alpha

控制UIView的透明度

backgroundColor

控制UIView的背景色

contentStretch

控制UIView的拉伸方式

通過設(shè)置這些屬性,基本上就解決了動(dòng)畫中的移動(dòng)到哪兒的問題。

接著,蘋果公司在UIView中加入很多方法來方便家控制動(dòng)畫的移動(dòng)時(shí)間,以及移動(dòng)的方式。iOS3.0及之前,UIView支持的Animation方法有如下這么多:

Object-c代碼

  1. @interface UIView(UIViewAnimation) 
  2.  
  3. + (void)beginAnimations:(NSString *)animationID context:(void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested 
  4. + (void)commitAnimations; // starts up any animations when the top level animation is commited 
  5.  
  6. // no getters. if called outside animation block, these setters have no effect. 
  7. + (void)setAnimationDelegate:(id)delegate; // default = nil 
  8. + (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context 
  9. + (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
  10. + (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2 
  11. + (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0 
  12. + (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date]) 
  13. + (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut 
  14. + (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional 
  15. + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero 
  16. + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default). 
  17.  
  18. + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // current limitation - only one per begin/commit block 
  19.  
  20. + (void)setAnimationsEnabled:(BOOL)enabled; // ignore any attribute changes while set. 
  21. + (BOOL)areAnimationsEnabled; 
  22.  
  23. @end 

這些方法非常的不直觀,開發(fā)者還是需要花很多時(shí)間去思考怎么組合這些方法。但是自從iOS4.0提供塊語法支持之后,蘋果公司把動(dòng)畫效果的實(shí)現(xiàn)封裝了一下,效果立桿見影,直觀了許多,因此大家完全可以不用去看上面的那些方法,重點(diǎn)關(guān)注如下的方法:

Object-c代碼

  1. @interface UIView(UIViewAnimationWithBlocks) 
  2.  
  3. + (void)animateWithDuration:(NSTimeInterval)duration  
  4. delay:(NSTimeInterval)delay  
  5. options:(UIViewAnimationOptions)options  
  6. animations:(void (^)(void))animations  
  7. completion:(void (^)(BOOL finished))completion; 
  8.  
  9. + (void)animateWithDuration:(NSTimeInterval)duration  
  10. animations:(void (^)(void))animations  
  11. completion:(void (^)(BOOL finished))completion  
  12. NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0 
  13.  
  14. + (void)animateWithDuration:(NSTimeInterval)duration  
  15. animations:(void (^)(void))animations 
  16. NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL 
  17.  
  18. + (void)transitionWithView:(UIView *)view  
  19. duration:(NSTimeIntervl)duration  
  20. options:(UIViewAnimationOptins)options  
  21. animations:(void (^)(void)animations  
  22. completion:(void (^)(BOOL finished) completion  
  23. NS_AVAILABLE_IOS(4_0); 
  24.  
  25. + (void)transitionFromView:(UIView *)fromView  
  26. toView:(UIView *)toView  
  27. duration:(NSTimeInterval)duration  
  28. options:(UIViewAnimationOptions)options  
  29. completion:(void (^)(BOOL finished))completion 
  30. NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview 
  31.  
  32. @end 

上面的幾個(gè)方法從名字上看就非常直觀。前三個(gè)方法都可以按如下的方式直譯,只是后兩個(gè)使用了一些默認(rèn)參數(shù)而已:

Java代碼

  1. 做一個(gè)動(dòng)畫效果,持續(xù)時(shí)間為duration,  
  2. 延遲delay秒開始執(zhí)行 , 
  3. 以options指定的方式運(yùn)行這個(gè)動(dòng)畫, 
  4. animations塊中指定哪些UIView會(huì)參加本次動(dòng)畫效果,以及動(dòng)畫效果完成時(shí)這些UIView會(huì)是一個(gè)什么狀態(tài),  
  5. 動(dòng)畫完成之后,執(zhí)行completion塊進(jìn)行收尾。 

有了這3個(gè)方法,開發(fā)者只需要思考,初始值,結(jié)果值,持續(xù)時(shí)間,運(yùn)行方式就行了,具體的細(xì)節(jié)移動(dòng)都交給類庫。

后2個(gè)方法是用于UIView相互之間轉(zhuǎn)換的,個(gè)人覺得用處不大,因?yàn)橛蒙厦娴娜齻€(gè)方法同樣可以做到這些效果,因此略過。

關(guān)于UIView的動(dòng)畫效果支持,有2點(diǎn)值得一提

* 上面所有的方法都是類方法,當(dāng)調(diào)用這些方法之后,系統(tǒng)會(huì)新起線程執(zhí)行動(dòng)畫效果,不會(huì)阻塞主線程的執(zhí)行。

* UIView的Animation效果只支持一些簡(jiǎn)單的2D動(dòng)畫效果,復(fù)雜的大家還得研究Core Animation。

一個(gè)實(shí)戰(zhàn)例子

在我寫的一個(gè)小游戲的主機(jī)界面中,我使用了一點(diǎn)動(dòng)畫的效果,主界面的設(shè)計(jì)圖如下:

[[81423]]

動(dòng)畫后的效果圖如下:

[[81424]]

我想要的效果就是,加載主界面后,圖片緩緩的展開成扇形,然后游戲的菜單顯示供玩家點(diǎn)擊。

代碼如下:

首先,準(zhǔn)備動(dòng)畫前狀態(tài),讓想展示的UIView不可見:

Object-c代碼

  1. -(void) prepareForIntroAnimation 
  2. self.sImageView.hidden=YES; 
  3. self.nImageView.hidden=YES; 
  4. self.aImageView.hidden=YES; 
  5. self.pImageView.hidden=YES; 
  6. self.jokerImageView.hidden=YES; 
  7.  
  8. self.hostGameButton.alpha=0.0f; 
  9. self.joinGameButton.alpha=0.0f; 
  10. self.singlePlayerGameButton.alpha=0.0f; 
  11. self.helpButton.alpha=0.0f; 
  12. _buttonsEnabled = NO; 

然后,展示動(dòng)畫效果:

Object-c代碼

  1. -(void) performAnimation 
  2. //顯示UIView 
  3. self.sImageView.hidden=NO; 
  4. self.nImageView.hidden=NO; 
  5. self.aImageView.hidden=NO; 
  6. self.pImageView.hidden=NO; 
  7. self.jokerImageView.hidden=NO; 
  8.  
  9. [UIView animateWithDuration:0.65f 
  10. delay:0.5f 
  11. options:UIViewAnimationOptionCurveEaseIn 
  12. animations:^ 
  13. //確定UIView的的中心位置和偏轉(zhuǎn)角度 
  14. self.sImageView.center = CGPointMake(80.0f, 108.0f); 
  15. self.sImageView.transform = CGAffineTransformMakeRotation(-0.22f); 
  16.  
  17. self.nImageView.center = CGPointMake(160.0f, 93.0f); 
  18. self.nImageView.transform = CGAffineTransformMakeRotation(-0.1f); 
  19.  
  20. self.aImageView.center = CGPointMake(240.0f, 88.0f); 
  21.  
  22. self.pImageView.center = CGPointMake(320.0f, 93.0f); 
  23. self.pImageView.transform = CGAffineTransformMakeRotation(0.1f); 
  24.  
  25. self.jokerImageView.center = CGPointMake(400.0f, 108.0f); 
  26. self.jokerImageView.transform = CGAffineTransformMakeRotation(0.22f); 
  27. completion:nil]; 
  28.  
  29. [UIView animateWithDuration:0.5f 
  30. delay:1.0f 
  31. options:UIViewAnimationOptionCurveEaseOut 
  32. animations:^ 
  33. //透明度設(shè)置為1,顯示游戲菜單。 
  34. self.hostGameButton.alpha = 1.0f; 
  35. self.joinGameButton.alpha = 1.0f; 
  36. self.singlePlayerGameButton.alpha = 1.0f; 
  37. self.helpButton.alpha = 1.0f; 
  38. completion:^(BOOL finished) 
  39. _buttonsEnabled = YES; 
  40. }]; 
  41.  

另外,動(dòng)畫效果還可以使用completion的回調(diào)塊做連接,完成多個(gè)動(dòng)畫效果的連接。

責(zé)任編輯:閆佳明 來源: apkbus
相關(guān)推薦

2011-08-22 14:21:24

iPhone開發(fā)UIView Anim

2011-06-28 10:23:27

UIViewiOS

2011-08-16 18:13:42

IPhone開發(fā)UIView動(dòng)畫

2012-12-24 13:38:01

iOSUIView

2015-07-27 10:27:32

IOS基礎(chǔ)知識(shí)核心動(dòng)畫

2011-07-03 10:05:52

Core Animat

2011-07-22 18:20:04

IOS View 動(dòng)畫

2011-08-11 10:16:23

iPhoneUIView視圖

2011-08-11 10:27:37

iPhoneUIView視圖

2011-06-13 15:00:00

Cocoa TouchiOS

2011-07-29 09:45:11

iOS 圖形圖像 Core Anima

2011-08-15 13:50:06

IPhone開發(fā)UIView動(dòng)畫

2011-07-03 10:12:35

Core Animat

2011-08-12 11:31:46

iPhoneUIView動(dòng)畫

2014-12-31 13:31:31

圖形動(dòng)畫翻頁

2015-01-19 12:19:04

iOS源碼ActionSheet仿QQ音樂

2011-07-03 10:16:45

Core Animat

2011-08-09 17:21:37

2015-03-18 09:29:12

iOS開發(fā)爭(zhēng)議

2013-06-04 15:41:31

iOS開發(fā)移動(dòng)開發(fā)block
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)