標籤:uiview執行基礎動畫animatio
在ios開發中一般用到的基礎動畫有以下幾種,所有的動畫參數配置大致相同,但是有時候在開發過程中很少這樣配置一般使用代碼塊比較方便,而且代碼也比較簡單以下是常用基礎動畫類型的一個配置
#pragma mark -- Action methods- (void)transitionAnimation// 轉場動畫{ //設定動畫名稱,方便代理方法判斷是哪個動畫 [UIView beginAnimations:@"TransitionAnimation" context:NULL]; //設定動畫時間長度 [UIView setAnimationDuration:3.0]; //設定動畫的變化規律 --有以下4中枚舉值 // UIViewAnimationCurveEaseInOut, 開始和結束減速 // slow at beginning and end// UIViewAnimationCurveEaseIn, // slow at beginning// UIViewAnimationCurveEaseOut, // slow at end// UIViewAnimationCurveLinear 勻速 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 對View設定轉場動畫方向 有以下枚舉方向// typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {// UIViewAnimationTransitionNone,// UIViewAnimationTransitionFlipFromLeft,// UIViewAnimationTransitionFlipFromRight,// UIViewAnimationTransitionCurlUp,// UIViewAnimationTransitionCurlDown,// }; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_view cache:NO]; // 對View設定初始狀態 並對其他進行配置(這裡只是讓View變成之前的參數而已,如果不需要就可以去掉這段代碼) [_view setTransform:CGAffineTransformIdentity]; [_view setBackgroundColor:[UIColor blackColor]]; [_view setAlpha:1]; [_view setCenter:CGPointMake(50, 50)]; // 設定代理 [UIView setAnimationDelegate:self]; // 動畫結束執行代理方法(這裡走得時代理方法也可以走其他動畫方法就可以形成動畫組) [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; //動畫結束 [UIView commitAnimations];}- (void)changeAlphaAnimation //改變透明度動畫{ [UIView beginAnimations:@"ChangeAlphaAnimation" context:NULL]; [UIView setAnimationDuration:2.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [_view setAlpha:0.2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; [UIView commitAnimations];}- (void)changeColorAnimation // 改變顏色動畫{ [UIView beginAnimations:@"ChangeColorAnimation" context:NULL]; [UIView setAnimationDuration:2.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [_view setBackgroundColor:[UIColor redColor]]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; [UIView commitAnimations];}- (void)rotationAnimation // 旋轉動畫{ [UIView beginAnimations:@"RotationAnimation" context:NULL]; [UIView setAnimationDuration:2.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [_view setTransform:CGAffineTransformRotate(_view.transform, M_PI_4)]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(changeColorAnimation)]; [UIView commitAnimations];}- (void)scareAnimation //放大縮小動畫{ [UIView beginAnimations:@"ScareAnimation" context:NULL]; [UIView setAnimationDuration:2.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDelegate:self]; [_view setTransform:CGAffineTransformScale(_view.transform, 2, 2)]; [UIView setAnimationDidStopSelector:@selector(rotationAnimation)]; [UIView commitAnimations];}- (void)positionAnimation //位移動畫{ [UIView beginAnimations:@"PositionAnition" context:NULL]; [UIView setAnimationDuration:2.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDelegate:self]; _view.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)); [UIView setAnimationDidStopSelector:@selector(scareAnimation)]; [UIView commitAnimations];}// 代理方法,檢測動畫介紹然後進行其他動作 還有其他兩個方法- (void)animationDidStop:(NSString *)animationId finished:(NSNumber *)finished context:(void *)context{ // 判斷是哪個動畫 然後執行相應操作 if ([animationId isEqualToString:@"ChangeColorAnimation"]) { [self changeAlphaAnimation]; } if ([animationId isEqualToString:@"ChangeAlphaAnimation"]) { [self transitionAnimation]; }}
下面我們來看下代碼塊的用法,代碼塊的話用起來很方便並且可以執行回調,在APP點擊菜動態單切換兩個View或是其他動畫
<pre name="code" class="objc">[UIView animateWithDuration:0.5 animations:^{ [UIView setAnimationDelay:0.8];//配置動畫時延 _currentView.center = CGPointMake(X,Y);//可以對多個view進行我們想要的動畫配置 newView.center = CGPointMake(X,Y); } completion:^(BOOL finished) { //執行完後走這裡的代碼塊 }];
ios之UIView執行基礎動畫Animation使用參數配置