iOS開發:UIView動畫詳解

來源:互聯網
上載者:User

iOS開發:UIView動畫詳解

   執行動畫所需要的工作由UIView類自動完成,但仍要在希望執行動畫時通知視圖,為此需要將改變屬性的程式碼封裝裝到一個代碼塊中。

  1.UIView動畫具體建立方法

  - (void)buttonPressed

  {

  // 交換本視圖控制器中2個view位置

  [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

  //UIView開始動畫,第一個參數是動畫的標識,第二個參數附加的應用程式資訊用來傳遞給動畫代理訊息

  [UIView beginAnimations:@"View Flip" context:nil];

  //動畫期間

  [UIView setAnimationDuration:1.25];

  //設定動畫的回呼函數,設定後可以使用回調方法

  [UIView setAnimationDelegate:self];

  //設定動畫曲線,控制動畫速度

  [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];

  //設定動畫方式,並指齣動畫發生的位置

  [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

  //提交UIView動畫

  [UIView commitAnimations];

  }

  - (void)viewDidLoad

  {

  [super viewDidLoad];

  //主要功能通過UIView動畫完成2個試圖控制器的切換

  self.blueController = [[BlueViewController alloc] initWithNibName:nil bundle:nil];

  //設定導航控制器view的大小占整個螢幕

  [self.blueController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];

  self.yellowController = [[YellowController alloc]initWithNibName:nil bundle:nil ];

  [self.yellowController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];

  //將2個控制器view插入到目前置航控制器視圖上,yellowController後插入,顯示在最前面

  [self.view insertSubview:self.blueController.view atIndex:0];

  [self.view insertSubview:self.yellowController.view atIndex:1];

  //建立導航控制器右按鈕,按鈕名字叫next

  //添加buttonPressed 事件

  self.rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(buttonPressed)];

  //將按鈕添加到導航控制器預設右按鈕上

  self.navigationItem.rightBarButtonItem = self.rightBarItem;

  }

  有個問題:如果動畫不放在按鈕事件中,直接放到viewDidLoad裡,程式首先執行這個controller,這時動畫是不會顯示的。

  原因:出現這個問題是因為開機時候系統有個動畫,系統動畫和這個動畫重複了。

  解決方案:

  1。將動畫寫在按鈕事件中

  2。利用定時器。

  areAnimationsEnabled

  返回一個布爾值表示動畫是否結束。

  + (BOOL)areAnimationsEnabled

  傳回值

  如果動畫結束返回YES,否則NO。

  beginAnimations:context:

  開始一個動畫塊

  + (void)beginAnimations:(NSString *)animationID context:(void *)context

  參數

  animationID

  動畫塊內部應用程式識別碼用來傳遞給動畫代理訊息-這個選取器運用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法來設定。

  context

  附加的應用程式資訊用來傳遞給動畫代理訊息-這個選取器使用setAnimationWillStartSelector: 和setAnimationDidStopSelector: 方法。

  討論

  這個值改變是因為設定了一些需要在動畫塊中產生動畫的屬性。動畫塊可以被嵌套。如果在沒有在動畫塊中調用那麼setAnimation類方法將什麼都不做。使用 beginAnimations:context:來開始一個動畫塊並用commitAnimations類方法來結束一個動畫塊。

  commitAnimations

  結束一個動畫塊並開始當他在動畫塊外時。

  + (void)commitAnimations

  討論

  如果當前的動畫塊是最外層的動畫塊,當應用程式返回到迴圈運行時開始動畫塊。動畫在一個獨立的線程中所有應用程式不會中斷。使用這個方法,多個動畫可以被實現。查看setAnimationBeginsFromCurrentState:來瞭解如果開始一個動畫當另外一個動畫在播放的時候。

  layerClass

  返回類用來建立這一個本類的layer執行個體對象。

  + (Class)layerClass

  傳回值

  一個用來建立視圖layer的類

  討論

  重寫子類來指定一個自訂類用來顯示。當在建立視圖layer時候調用。預設的值是CALayer類對象。

  setAnimationBeginsFromCurrentState

  :

  設定動畫從目前狀態開始播放。

  + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState

  參數

  fromCurrentState

  YES如果動畫需要從他們目前狀態開始播放。否則為NO。

  討論

  如果設定為YES那麼當動畫在運行過程中,當前視圖的位置將會作為新的動畫的開始狀態。如果設定為NO,當前動畫結束前新動畫將使用視圖最後狀態的位置作 為開始狀態。這個方法將不會做任何事情如果動畫沒有運行或者沒有在動畫塊外調用。使用beginAnimations:context:類方法來開始並用 commitAnimations類方法來結束動畫塊。預設值是NO。

  setAnimationCurve

  :

  設定動畫塊中的動畫屬性變化的曲線。

  + (void)setAnimationCurve:(UIViewAnimationCurve)curve

  討論

  動畫曲線是動畫運行過程中相對的速度。如果在動畫塊外調用這個方法將會無效。使用 beginAnimations:context:類方法來開始動畫塊並用commitAnimations來結束動畫塊。預設動畫曲線的值是 UIViewAnimationCurveEaseInOut。

  setAnimationDelay:

  在動畫塊中設定動畫的延遲屬性(以秒為單位)

  + (void)setAnimationDelay:(NSTimeInterval)delay

  討論

  這個方法在動畫塊外調用無效。使用beginAnimations:context: 類方法開始一個動畫塊並用commitAnimations類方法結束動畫塊。預設的動畫延遲是0.0秒。

  setAnimationDelegate:

  設定動畫訊息的代理。

  + (void)setAnimationDelegate:(id)delegate

  參數

  delegate

  你可以用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法來設定接收代理訊息的對象。

  討論

  這個方法在動畫塊外沒有任何效果。使用beginAnimations:context:類方法開始一個動畫塊並用commitAnimations類方法結束一個動畫塊。預設值是nil

  setAnimationDidStopSelector:

  設定訊息給動畫代理當動畫停止的時候。

  + (void)setAnimationDidStopSelector:(SEL)selector

  參數

  selector

  當動畫結束的時候發送給動畫代理。預設值是NULL。這個選擇者須有下面方法的簽名:animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context。

  animationID

  一個應用程式提供的標識符。和傳給beginAnimations:context: 相同的參數。這個參數可以為空白。

  finished

  如果動畫在停止前完成那返回YES;否則就是NO。

  context

  一個可選的應用程式內容提供者。和beginAnimations:context: 方法相同的參數。可以為空白。

  討論

  這個方法在動畫塊外沒有任何效果。使用beginAnimations:context: 類方法來開始一個動畫塊並用commitAnimations類方法結束。預設值是NULL。

  setAnimationDuration:

  設定動畫塊中的動畫期間(用秒)

  + (void)setAnimationDuration:(NSTimeInterval)duration

  參數

  duration

  一段動畫持續的時間。

  討論

  這個方法在動畫塊外沒有效果。使用beginAnimations:context: 類方法來開始一個動畫塊並用commitAnimations類方法來結束一個動畫塊。預設值是0.2。

  setAnimationRepeatAutoreverses:

  設定動畫塊中的動畫效果是否自動重複播放。

  + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses

  參數

  repeatAutoreverses

  如果動畫自動重複就是YES否則就是NO。

  討論

  自動重複是當動畫向前播放結束後再重頭開始播放。使用setAnimationRepeatCount: 類方法來指定動畫自動重播的時間。如果重複數為0或者在動畫塊外那將沒有任何效果。使用beginAnimations:context:類方法來開始一個動畫塊並用commitAnimations方法來結束一個動畫塊。預設值是NO。

  setAnimationRepeatCount:

  設定動畫在動畫模組中的重複次數

  + (void)setAnimationRepeatCount:(float)repeatCount

  參數

  repeatCount

  動畫重複的次數,這個值可以是分數。

  討論

  這個屬性在動畫塊外沒有任何作用。使用beginAnimations:context:類方法來開始一個動畫塊並用commitAnimations類方法來結束。預設動畫不迴圈。

  setAnimationsEnabled:

  設定是否啟用動畫

  + (void)setAnimationsEnabled:(BOOL)enabled

  參數

  enabled

  如果是YES那就啟用動畫;否則就是NO

  討論

  當動畫參數沒有被啟用那麼動畫屬性的改變將被忽略。預設動畫是被啟用的。

  setAnimationStartDate:

  設定在動畫塊內部動畫屬性改變的開始時間

  + (void)setAnimationStartDate:(NSDate *)startTime

  參數

  startTime

  一個開始動畫的時間

  討論

  使用beginAnimations:context:類方法來開始一個動畫塊並用commitAnimations類方法來結束動畫塊。預設的開始時間值由CFAbsoluteTimeGetCurrent方法來返回。

  setAnimationTransition:forView:cache:

  在動畫塊中為視圖設定過渡

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

  參數

  transition

  把一個過渡效果應用到視圖中。可能的值定義在UIViewAnimationTransition中。

  view

  需要過渡的視圖對象。

  cache

  如果是YES,那麼在開始和結束圖片視圖渲染一次並在動畫中建立幀;否則,視圖將會在每一幀都渲染。例如緩衝,你不需要在視圖轉變中不停的更新,你只需要等到轉換完成再去更新視圖。

  討論

  如果你想要在轉變過程中改變視圖的外貌。舉個例子,檔案從一個視圖到另一個視圖,然後使用一個UIView子類的[內容] 檢視,如下:

  1.Begin an animation block.

  2.Set the transition on the container view.

  3.Remove the subview from the container view.

  4.Add the new subview to the container view.

  5.Commit the animation block.

  1.開始一個動畫塊。 2.在[內容] 檢視中設定轉換。 3.在[內容] 檢視中移除子視圖。 4.在[內容] 檢視中添加子視圖。 5.結束動畫塊。

  setAnimationWillStartSelector:

  當動畫開始時發送一條訊息到動畫代理

  + (void)setAnimationWillStartSelector:(SEL)selector

  參數

  selector

  在動畫開始前向動畫代理髮送訊息。預設值是NULL。這個selector必須由和beginAnimations:context: 方法相同的參數,一個任選的程式標識和內容。這些參數都可以是nil。

  討論

  這個方法在動畫塊外沒有任何作用。使用beginAnimations:context:類方法來開始一個動畫塊並用commitAnimations類方法來結束。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.