UIKit 和 Core Animation 都對 animation 提供支持。在UIKit中,動畫是用UIView對象展現。ios
Animatable UIView properties:動畫
1.frame :改變view 的大小和該view在superview中的相對位置。url
2.bounds:改變view的大小(尺寸)。spa
3.center :改變view 相對於superview座標系統的位置。線程
4.transform:控制UIView的縮放,旋轉角度等固定好中心位置以後的變化(這個屬性應用於2D空間的展現,若是要展現3D動畫,則必須使用Core Animation)code
5.alpha :改變View 的透明度orm
6.backgroundColor:改變 view 的背景顏色對象
7.contentStretch:改變view 的拉伸方式blog
在ios4 之後,使用block-based 類方法來建立動畫:事件
animateWithDuration: animations:
animateWithDuration: animations: completion:
animateWithDuration: delay : options: animations: completion:
由於是類方法,因此你建立的動畫代碼塊不單單是針對一個view。所以,你能夠用這些方法建立一個包含多個 view 改變的動畫。例如:
1 [UIView animateWithDuration:3.0 animations:^{ 2 firstView.alpha = 0.25; 3 secondView.alpha = 1.0; 4 }];
當上面代碼執行的時候,動畫當即在另外一個線程中執行,也就避免了阻塞當前線程或者主線程。
剛纔的代碼中只使用了淡入,淡出的動畫效果。若是你想改變默認的動畫參數,你必須使用animateWithDuration: delay: options: animations: completion: 這個方法來建立動畫。這個方法可讓你自定義以下動畫參數:
1.動畫開始之前的延遲時間。
2.動畫運行的時間曲線。
3.動畫重複的次數。
4.當動畫結束的時候是否自動的反向運行動畫
5.當動畫運行的時候,是否觸摸事件能夠傳遞到view
6.是否動畫能夠中斷其餘正在運行的動畫或者直到其餘的其餘動畫結束才能夠開始運行
使用Begin/Commit 方法來開始動畫:
若是你的程序運行在 ios 3.2 或者更早的系統, 你必須使用 beginAnimations: context: 和 commitAnimations 的類方法來定義你的動畫塊。如:
1 [UIView beginAnimations:@"ToggleViews" context:nil]; 2 [UIView setAnimationDuration:1.0]; 3 firstView.alpha = 0.25; 4 secondView.alpha = 1.0; 5 6 [UIView commitAnimations];
你能夠爲Begin/Commit方法配置以下參數:
1 + (void)setAnimationDelegate:(id)delegate; // default = nil 2 + (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context 3 + (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 4 + (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2 5 + (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0 6 + (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date]) 7 + (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut 8 + (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional 9 + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero 10 + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).
移動動畫的例子:
1 CGRect rect = _moveView.frame; 2 if(_count%4==0) 3 rect.origin.x = 240; 4 else if(_count%4==1) 5 rect.origin.y = 336; 6 else if(_count%4==2) 7 rect.origin.x = 0; 8 else if(_count%4==3) 9 rect.origin.y = 0; 10 11 [UIView animateWithDuration:1 animations:^{ 12 _moveView.frame = rect; 13 } completion:^(BOOL finished) { 14 _count++; 15 16 }];
漸變更畫的例子:
1 [UIView animateWithDuration:1 animations:^{ 2 _view1.alpha = 0; 3 4 } completion:^(BOOL finished) { 5 if(_isChanged) 6 { 7 _view1.backgroundColor = [UIColor purpleColor]; 8 } 9 else 10 { 11 _view1.backgroundColor = [UIColor greenColor]; 12 } 13 [UIView animateWithDuration:1 animations:^{ 14 _view1.alpha = 1; 15 } completion:^(BOOL finished) { 16 _isChanged = !_isChanged; 17 }]; 18 } ];
旋轉動畫的例子:
1 [UIView animateWithDuration:1 animations:^{ 2 _view1.transform = CGAffineTransformMakeRotation((10*_count)%360*M_PI/180); 3 } completion:^(BOOL finished) { 4 _count++; 5 }];
翻轉動畫的例子:
1 [UIView animateWithDuration:1 animations:^{ 2 if(!_isChanged) 3 { 4 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight 5 forView:_view1 6 cache:YES]; 7 [_view1 addSubview:_view2]; 8 } 9 else 10 { 11 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft 12 forView:_view1 13 cache:YES]; 14 [_view2 removeFromSuperview]; 15 } 16 } completion:^(BOOL finished) { 17 _isChanged = !_isChanged; 18 }];
翻頁動畫的例子:
1 [UIView animateWithDuration:1 animations:^{ 2 if(!_isChanged) 3 { 4 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp 5 forView:_view1 6 cache:YES]; 7 [_view1 addSubview:_view2]; 8 } 9 else 10 { 11 [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 12 forView:_view1 13 cache:YES]; 14 [_view2 removeFromSuperview]; 15 } 16 } completion:^(BOOL finished) { 17 _isChanged = !_isChanged; 18 }];