UIKit通過封裝Core Animation實現了一些常用的動畫效果,用起來非常方便。使用的方法是通過UIView來聲明一個動畫塊,在這個塊中做的任何屬性變化,都會呈現動畫效果。
具體的文法上有兩種寫法,這裡講的是老式的寫法,IOS4.0後的新寫法請參考文檔,基本的思路是一樣的。具體可以看這個:
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html
先說明幾個基本的概念,方便理解後面的函數。
屬性變化: 可以實現動畫效果的屬性包括位置(frame, bound), 對齊關係,透明度,背景色,內容展開,和transform(這個就多了,下面講)
timing curve: 時間曲線,以時間作為橫軸,其他值(這裡就是指需要變化的屬性)作為縱軸。在整個動畫期間內的函數曲線。
ease in/ease out: 慢進/慢出,結合上面的時間曲線的概念,就是在動畫開始/或是結束的時候,屬性變化會減慢,看下面這個圖:是ease in ease out 也是預設的動畫效果(不是很好,網上隨便找的)
liner: 線性變化,這個不講了,時間變化曲線一共就這兩種。預設是EaseInEaseOut,無疑EaseInEaseOut的效果會更加平滑,但是負荷也大些,不過一般問題不大。
fade in /fade out: 淡入, 淡出,是一種動畫效果,就是逐漸消失,逐漸出現這種東西。
講具體的函數前,先舉個例子先,
代碼
[UIView beginAnimations:@"ToggleViews" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationEaseInOut];
// Make the animatable changes.
firstView.alpha = 0.0;
secondView.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
這段代碼就可以實現一個漂亮的淡入淡出的切換了,你所要做的,就是用begin/commit函數圈起一塊地區,然後把你想做的變化寫進去,無論有多少個,他們都會不被立刻執行,知道commit函數提交。簡單的說明下函數:
beginAnimation:context: 兩個參數都是給delegate用的,一般nil也沒問題,animationID是標示當前動畫的名稱,在一個代理對應多端動畫時用於區別,context是void*,回呼函數裡常用,用於傳遞額外的資料,儲存上下文,避免使用全域變數。
setAnimationCurve: 這個上面說過了,預設就是UIViewAnimationCurveEaseInOut,不寫也可以。
setAnimationDuration: 動畫的長度,秒作為單位
再補充個常用的函數,setAnimationRepeatCount: 可以重複動畫,有些情境下挺好用的。
如果需要在動畫之前或是動畫之後做一些操作的話,可以定義代理(就是兩個回呼函數)。看下面這個例子,在一個動畫後面接了另外一個動畫,熟悉代理的使用話,就沒啥可講的了。
代碼
// This method begins the first animation.
- (IBAction)showHideView:(id)sender
{
[UIView beginAnimations:@"ShowHideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];
// Make the animatable changes.
thirdView.alpha = 0.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
// Called at the end of the preceding animation.
- (void)showHideDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[UIView beginAnimations:@"ShowHideView2" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelay:1.0];
thirdView.alpha = 1.0;
[UIView commitAnimations];
}
接下去要講view transition的動畫效果,就是常見的那種翻頁的效果,老版本的寫法是這樣的:
代碼
//代碼摘自iphone基礎開發
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
[yellowViewController viewDidDisappear:YES];
[blueViewController viewDidAppear:YES];
在IOS4.0 之前,要實現view之間切換的動畫效果你必須使用父view,然後切換子view,只有子view的效果才能出現動畫,所以你看setAnimationTranistion裡forView裡寫的父view。
4.0之後,可以這麼寫:
代碼
[UIView transitionFromView:(self.view)
toView:(self._view2)
duration:1.0
options:UIViewAnimationOptionTransitionCurlUp
completion:^(BOOL finished) {
}
];