iOS開發之動畫編程的幾種方法,ios開發幾種方法
iOS開發之動畫編程的幾種方法
IOS中的動畫總結來說有五種:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKeyframeAnimation>,NSTimer
這裡我就總結了一下這五種方法,其實iOS開發中動畫的編程都會在這裡面變化,所以只要弄懂了這些動畫編程就不難了。
一:UIView動畫一般方式
- typedef enum {
- UIViewAnimationTransitionNone, //普通狀態
- UIViewAnimationTransitionFlipFromLeft, //從左往右翻轉
- UIViewAnimationTransitionFlipFromRight, //從右往左翻轉
- UIViewAnimationTransitionCurlUp, //向上翻頁
- UIViewAnimationTransitionCurlDown, //向下翻頁
- } UIViewAnimationTransition;
- typedef enum {
- UIViewAnimationCurveEaseInOut,
- UIViewAnimationCurveEaseIn,
- UIViewAnimationCurveEaseOut,
- UIViewAnimationCurveLinear
- } UIViewAnimationCurve;
這裡我實現了一個自訂的動畫方法,方便使用,只需要調用就可以實現很好的功能。
方法的實現
-(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];
Block方式
進階一點的block動畫(Next)內嵌
- (void)changeUIView{ [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(void){ moveView.alpha = 0.0; }completion:^(BOOL finished){ [UIView animateWithDuration:1 delay:1.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^(void){ [UIView setAnimationRepeatCount:2.5]; moveView.alpha = 1.0; }completion:^(BOOL finished){ }]; }]; }
二:.CAAnimation
需要添加庫,和包含標頭檔
caanimation有多個子類
CABasicAnimation
CAKeyframeAnimation
CATransition
- /*
- kCATransitionFade;
- kCATransitionMoveIn;
- kCATransitionPush;
- kCATransitionReveal;
- */
- /*
- kCATransitionFromRight;
- kCATransitionFromLeft;
- kCATransitionFromTop;
- kCATransitionFromBottom;
- */
- /*
- cube
- suckEffect 捲走
- oglFlip 翻轉
- rippleEffect 水波
- pageCurl 翻頁
- pageUnCurl
- cameraIrisHollowOpen
- cameraIrisHollowClose
- */
三:NSTimer
這是一種定時器來操作動畫的方法,他可以結合上面的方法來實現動畫的多樣化!
}