您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

自己寫的動畫封裝函式,重用超方便

編輯:iOS 開發綜合

做 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 則是動畫時間。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved