2012/3/1

iOS的動畫控製

iOS的動畫
如果要在iOS系統上做動化,有幾個方法可以來做
1、用CATranscation
2、用CATransition,幾個常用的CAAnimation就是透過這種方式
3、用UIView的




幾個常用的類別
  1. CALayer
  2. CATranscation
  3. CATransition:CAAnimation
CALayer,類似View,嚴格來講,view是由layer組成,簡單來說 ,CALayer是負責將畫面 show出來,另外,rootLayer是不能跑動畫的,
CATranscation,負責控制layer的移動,變型,...可透過CATranscation來寫自已要的動畫腳本

CAAnimation,子類別有CABasicAnimation、CATransition…等已經寫好的動畫,直接設定參數就可以使用
CATransition,繼承至CAAnimation,動畫效果,如淡入淡出


----------------------------------------------------------------------------------------------------
1、用CATranscation做動畫
用CATranscation實作動化,只能對第二層的layer作用,也就是view.layer是第一層,只能對這個layer底下的sublayer產生動畫效果,請注意

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithDouble:5.0] forKey:kCATransactionAnimationDuration];
// Position and rotate the listener
myLayer.position = touchPoint;
//_listenerLayer.transform = CATransform3DMakeRotation(playback.listenerRotation, 0., 0., 1.);
//m = CATransform3DMakeRotation(rot, 0., 0., 1.);
[CATransaction commit]; 

2、用CATransition做動畫

CATransition的主要type有四
kCATransitionMoveIn, 淡出,淡入+動畫
kCATransitionPush,淡出+移動,淡入+移動
kCATransitionReveal, 淡出+動畫,淡入
kCATransitionFade,淡入,淡出

subtype決定動畫的移動方向

kCATransitionFromLeft,
kCATransitionFromRight,
kCATransitionFromTop,
kCATransitionFromBottom

kCATransitionFade是不需要subtype的

ex
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
NSString *types[4] = {kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};
NSString *subtypes[4] = {kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};
int rnd = random() % 4;
transition.type=kCATransitionMoveIn;
transition.delegate = self;
[imgView.layer addAnimation:transition forKey:nil];
CGPoint touchPoint = [touch locationInView:self];
[imgView setCenter:touchPoint];
用CABaiscAnimation做動畫
//同時對不同軸做旋轉
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.duration = 3;
animation.repeatCount = 1;
animation.fromValue=[NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:M_PI*2];
//[imgView.layer addAnimation:animation forKey:@"transform.rotation.y"];
[imgView.layer addAnimation:animation forKey:kCATransition];

3、用UIView的beginAnimation來製作動畫

在執行動畫的時候,可以用以下method來讓動化的初始狀態為目前的狀態
[UIView setAnimationBeginsFromCurrentState:YES];
如以下範例

移動動畫
[UIView beginAnimations:nil context:NULL];//動畫委配
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:1];//delay 1 sec 
childView.center = [touch locationInView:self];
[UIView commitAnimations];

變型動畫
ex1
childView.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];//動畫委配
childView.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
ex2
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS];
placardView.transform = CGAffineTransformMakeScale(1.1f, 1.1f); 
NSValue *touchPointValue = (NSValue *)context;
placardView.center = [touchPointValue CGPointValue];
[touchPointValue release];
[UIView commitAnimations];
ex3
[UIView beginAnimations:nil context:NULL];//動畫委配
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];//delay 1 sec 
CATransform3D t;
t=imgView.layer.transform;
t=CATransform3DRotate(t, radians(180), 0, 1, 0);
imgView.layer.transform=t;
[UIView commitAnimations];

沒有留言: