做 iOS 動畫時,每次都重複寫 begin/commit 那一串實在麻煩。乾脆把它封裝成一個自訂方法,之後只要傳入視圖、目標 frame、動畫曲線、透明度與時長就能一行搞定。
-(void)UIViewAnimation:(UIView* )view
frame:(CGRect)frame
type:(int)type
alpha:(float)alpha
duration:(float)duration
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:type];
[UIView setAnimationDelegate:self];
view.alpha=alpha;
view.frame=frame;
[UIView commitAnimations];
}
呼叫方式
[self UIViewAnimation:downView frame:CGRectMake(0, height, 320, 58) type:UIViewAnimationCurveEaseOut alpha:1 duration:0.3];
其中 downView 是你要做動畫的視圖,
CGRectMake(0, height, 320, 58) 決定位置變換,
UIViewAnimationCurveEaseOut 決定動畫效果,
動畫曲線還有以下幾種可選:
UIViewAnimationCurveEaseInOut, // 開頭與結尾都較慢
UIViewAnimationCurveEaseIn, // 開頭較慢
UIViewAnimationCurveEaseOut, // 結尾較慢
UIViewAnimationCurveLinear
最後 alpha 控制透明度變化,duration 則是動畫時間。