天天看點

UIViewAnimation動畫與CATransition類動畫

CATransition

CATransition is an Objective-C wrapper for creating view transitions. As of 3.1.2, there are 11 types of transitions. 4 of them are SDK-compatible, but are the most boring ones. The following shows all 11 types of transition from picture "A" to "B" at 40%. The subtypes, if any, is "fromLeft".

UIViewAnimation動畫與CATransition類動畫

cameraIris

UIViewAnimation動畫與CATransition類動畫

cube

UIViewAnimation動畫與CATransition類動畫

fade (kCATransitionFade)

UIViewAnimation動畫與CATransition類動畫

moveIn (kCATransitionMoveIn)

UIViewAnimation動畫與CATransition類動畫

oglFlip

UIViewAnimation動畫與CATransition類動畫

pageCurl

UIViewAnimation動畫與CATransition類動畫

pageUnCurl

UIViewAnimation動畫與CATransition類動畫

push (kCATransitionPush)

UIViewAnimation動畫與CATransition類動畫

reveal (kCATransitionReveal)

UIViewAnimation動畫與CATransition類動畫

rippleEffect

UIViewAnimation動畫與CATransition類動畫

suckEffect

Main article: CAFilter

Many transitions have are further divided into several discrete subtypes. They often control the movement direction of the animation.

Some transitions accept addition arguments through the filter property, for example, you can set the location of suckEffect using

Transition

Subtypes

Accepted parameters

moveIn

push

reveal

fromLeft, fromRight, fromBottom, fromTop

-

pageCurl, pageUnCurl

fromLeft, fromRight, fromTop, fromBottom

float inputColor[];

alignedCube

fromLeft, fromRight, fromTop, fromBottom

float inputAmount; (perspective)

flip

alignedFlip

float inputAmount;

CGPoint inputPosition;

rotate

90cw, 90ccw, 180cw, 180ccw

The following shows the availability of different CATransitions starting from 2.0

Availability

fade

(Public API) 2.0–

2.0–

cameraIrisHollowOpen

cameraIrisHollowClose

4.0–

spewEffect

genieEffect

unGenieEffect

twist

swirl

charminUltra

reflection

zoomyIn

zoomyOut 

mapCurl

mapUnCurl

oglApplicationSuspend

cameraIrisHollow

2.0–2.2

Official reference: CATransition

Header: http://github.com/kennytm/iphone-private-frameworks/blob/master/QuartzCore/CATransition2.h

UIViewAnimation動畫與Core Animation的CATransition類動畫

1.使用UIView類函數實作:

//UIViewAnimationTransitionFlipFromLeft, 向左轉動

//UIViewAnimationTransitionFlipFromRight, 向右轉動

//UIViewAnimationTransitionCurlUp, 向上翻動

//UIViewAnimationTransitionCurlDown, 向下翻動

[UIView beginAnimations:@"animationID" context:nil];

[UIView setAnimationDuration:0.5f]; //動畫時長

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; //給視圖添加過渡效果

//在這裡寫你的代碼.

[UIView commitAnimations]; //送出動畫

2.使用CATransition對象來實作:

CATransition比較強大,一般可以使用CATransition模拟UIView的動畫。

    /* 過渡效果

fade     //交叉淡化過渡(不支援過渡方向)

push     //新視圖把舊視圖推出去

moveIn   //新視圖移到舊視圖上面

reveal   //将舊視圖移開,顯示下面的新視圖

cube     //立方體翻滾效果

oglFlip  //上下左右翻轉效果

suckEffect   //收縮效果,如一塊布被抽走(不支援過渡方向)

rippleEffect //滴水效果(不支援過渡方向)

pageCurl     //向上翻頁效果

pageUnCurl   //向下翻頁效果

cameraIrisHollowOpen  //相機鏡頭打開效果(不支援過渡方向)

cameraIrisHollowClose //相機鏡頭關上效果(不支援過渡方向)

*/

/* 過渡方向

fromRight;

fromLeft;

fromTop;

fromBottom;

CATransition *animation = [CATransition animation];

animation.delegate = self;

animation.duration = 0.5f; //動畫時長

animation.timingFunction = UIViewAnimationCurveEaseInOut;

animation.fillMode = kCAFillModeForwards;

animation.type = @”cube”; //過度效果

animation.subtype = @”formLeft”; //過渡方向

animation.startProgress = 0.0 //動畫開始起點(在整體動畫的百分比)

animation.endProgress = 1.0;  //動畫停止終點(在整體動畫的百分比)

animation.removedOnCompletion = NO;

[self.view.layer addAnimation:animation forKey:@"animation"];

實作iPhone漂亮的動畫效果主要有兩種方法:

  一種是UIView層面的,

  一種是使用CATransition進行更低層次的控制,

  第一種是UIView,UIView方式可能在低層也是使用CATransition進行了封裝,它隻能用于一些簡單的、常用的效果展現,這裡寫一個常用的示例代碼,供大家參考。

  Cpp代碼

  [UIView beginAnimations:@"Curl"context:nil];//動畫開始

  [UIView setAnimationDuration:0.75];

  [UIView setAnimationDelegate:self];

  [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES];

  [myview removeFromSuperview];

  [UIView commitAnimations];

  第二種方式相對複雜一些,但如果更好的進行控制,還是使用這種方法吧,

  基本使用方法可以看一下如下例子:

  CATransition *animation = [CATransition animation];

  [animation setDuration:1.25f];

  [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

  [animation setType:kCATransitionReveal];

  [animation setSubtype: kCATransitionFromBottom];

  [self.view.layer addAnimation:animation forKey:@"Reveal"];

  這裡使用了setType與setSubtype組合,這使用個比較保險,因為他的參數就是官方API裡定義的,他們的參數說明可以參考如下:

  [animation setType:@"suckEffect"];

  這裡的suckEffect就是效果名稱,可以用的效果主要有:

  pageCurl 向上翻一頁

  pageUnCurl 向下翻一頁

  rippleEffect 滴水效果

  suckEffect 收縮效果,如一塊布被抽走

  cube 立方體效果

  oglFlip 上下翻轉效果

iphone中CABasicAnimation和UIView動畫的差別[轉]

關于UIView動畫:

[UIView beginAnimations:@"zoom out" context:nil];

[UIView setAnimationDuration:1.f];

[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

cover.transform = CGAffineTransformMakeScale(9.25,7.05);

cover.center = CGPointMake(430, 512);

[UIView commitAnimations]

UIView動畫是應用在一個view上面的。

關于CABasicAnimation動畫:

- (CAAnimation *)animationMove:(CGPoint)rootCenter

{

    CABasicAnimation *animationMove

    = [CABasicAnimation animationWithKeyPath:@"position"];

    animationMove.duration = 1;

    animationMove.autoreverses = NO;

//    animationMove.delegate = self;

    animationMove.removedOnCompletion = NO;

    animationMove.fillMode = kCAFillModeForwards;

    animationMove.fromValue = [NSValue valueWithCGPoint:self.oldCoverCenter];

    animationMove.toValue =[NSValue valueWithCGPoint:rootCenter];

    return animationMove;

}

CABasicAnimation動畫是應用在一個layer上面的。

注:

1,把一個image放在一個view的layer上來放大的時候,如果用UIView來做,圖檔不會太多的失真和閃爍的效果,但是用CABasicAnimation來做失真和閃爍現象會很嚴重,效果很不好。

2,做 動畫的疊加效果 很簡單,隻要把各自的動畫放在一起就可以了。請看這個效果:一本書邊移動到螢幕中間,邊放大,邊打開封面的效果。

[imageLayer addAnimation:[self animationOpen] forKey:@"Open"];

cover.transform = CGAffineTransformMakeScale(5.5,5.5);

cover.center = CGPointMake(629, 384);

[UIView commitAnimations];

- (CAAnimation *)animationOpen

    CABasicAnimation *animationOpen

    = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

    animationOpen.duration = 1;

    animationOpen.autoreverses = NO;

    animationOpen.delegate = self;  //然後執行真正地打開書的内容

    animationOpen.removedOnCompletion = NO;

    animationOpen.fillMode = kCAFillModeForwards;

    animationOpen.fromValue = [NSNumber numberWithFloat:-M_PI/5];

    animationOpen.toValue = [NSNumber numberWithFloat:-M_PI/1.5];

    return animationOpen;