標籤:
CATransactions CATransaction 事務類,可以對多個layer的屬性同時進行修改.它分隱含交易,和明確交易.
區分隱式動畫和隱含交易:隱式動畫通過隱含交易實現動畫 。
區分顯式動畫和明確交易:顯式動畫有多種實現方式,明確交易是一種實現顯式動畫的方式。
1.隱含交易
除明確交易外,任何對於CALayer屬性的修改,都是隱含交易.這樣的事務會在run-loop中被提交.
- (void)viewDidLoad {
//初始化一個layer,添加到主視圖
layer=[CALayer layer];
layer.bounds = CGRectMake(0, 0, 200, 200);
layer.position = CGPointMake(160, 250);
layer.backgroundColor = [UIColor redColor].CGColor;
layer.borderColor = [UIColor blackColor].CGColor;
layer.opacity = 1.0f;
[self.view.layer addSublayer:layer];
[super viewDidLoad];
}
-(IBAction)changeLayerProperty
{
//設定變化動畫過程是否顯示,預設為YES不顯示
[CATransaction setDisableActions:NO];
//設定圓角
layer.cornerRadius = (layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
//設定透明度
layer.opacity = (layer.opacity == 1.0f) ? 0.5f : 1.0f;
}
111
2. 明確交易
通過明確的調用begin,commit來提交動畫
修改執行時間
[CATransaction begin];
//明確交易預設開啟動畫效果,kCFBooleanTrue關閉
[CATransaction setValue:(id)kCFBooleanFalse
forKey:kCATransactionDisableActions];
//動畫執行時間
[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration];
//[CATransaction setAnimationDuration:[NSNumber numberWithFloat:5.0f]];
anotherLayer.cornerRadius = (anotherLayer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
layer.opacity = (layer.opacity == 1.0f) ? 0.5f : 1.0f;
[CATransaction commit];
事務嵌套 [CATransaction begin]; [CATransaction begin]; [CATransaction setDisableActions:YES]; layer.cornerRadius = (layer.cornerRadius == 0.0f) ? 30.0f : 0.0f; [CATransaction commit]; //上面的動畫並不會立即執行,需要等最外層的commit [NSThread sleepForTimeInterval:10]; //明確交易預設開啟動畫效果,kCFBooleanTrue關閉 [CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions]; //動畫執行時間 [CATransaction setValue:[NSNumber numberWithFloat:10.0f] forKey:kCATransactionAnimationDuration]; //[CATransaction setAnimationDuration:[NSNumber numberWithFloat:5.0f]]; anotherLayer.cornerRadius = (anotherLayer.cornerRadius == 0.0f) ? 30.0f : 0.0f; [CATransaction commit];
CATranstion:提供了影響整個層內容過渡的效果,在動畫期間它使層產生fade(漸層),push(推拉)以及reveals(揭示)的動畫效果。這些過渡的效果可以通過你自己自訂的core image filters來擴充。
引用:http://hi.baidu.com/vbkan/blog/item/8b3314ca1b1da85ef31fe7d4.html
http://blog.sina.com.cn/s/blog_74d748180100qi8e.html
實現iphone漂亮的動畫效果主要有兩種方法,一種是UIView層面的,一種是使用CATransition進行更低層次的控制,
第一種是UIView,UIView方式可能在低層也是使用CATransition進行了封裝,它只能用於一些簡單的、常用的效果展現,這裡寫一個常用的範例程式碼,供大家參考。
| |
[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裡定義的,他們的參數說明可以參考如下(預設類型是淡入淡出類型。):
| |
setType:可以返回四種類型:
kCATransitionFade 淡出
kCATransitionMoveIn 覆蓋原圖
kCATransitionPush 推出
kCATransitionReveal底部顯出來
setSubtype:也可以有四種類型:
kCATransitionFromRight;
kCATransitionFromLeft(預設值)
kCATransitionFromTop;
kCATransitionFromBottom |
還有一種設定動畫類型的方法,不用setSubtype,只用setType
|
|
[animation setType:@"suckEffect"]; |
這裡的suckEffect就是效果名稱,可以用的效果主要有:
|
|
pageCurl 向上翻一頁
pageUnCurl 向下翻一頁
rippleEffect 滴水效果
suckEffect 收縮效果,如一塊布被抽走
cube 立方體效果
oglFlip 上下翻轉效果 |
最後再給出一種常用代碼供大家參考。
範例程式碼一:
// Curl the image up or down
CATransition *animation = [CATransition animation];
[animation setDuration:0.35];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
if (!curled){
//animation.type = @"mapCurl";
animation.type = @"pageCurl";
animation.fillMode = kCAFillModeForwards;
animation.endProgress = 0.99;
} else {
//animation.type = @"mapUnCurl";
animation.type = @"pageUnCurl";
animation.fillMode = kCAFillModeBackwards;
animation.startProgress = 0.01;
}
[animation setRemovedOnCompletion:NO];
[view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[view addAnimation:animation forKey"pageCurlAnimation"];
// Disable user interaction where necessary
if (!curled) {
} else {
}
urled = !curled;
範例程式碼二:
頭部匯入: #import <QuartzCore/QuartzCore.h>
- (void) startAction:(id) sender{
// 定義動畫
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration =1.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//可根據需要,設定type和subtype屬性產生不同的動畫效果
//animation.type = kCATransitionPush;
//animation.type = kCATransitionMoveIn;
//animation.type = kCATransitionReveal;
//animation.type = kCATransitionFade;
//animation.subtype = kCATransitionPush;
//animation.subtype = kCATransitionMoveIn;
//animation.subtype = kCATransitionReveal;
//animation.subtype = kCATransitionFade;
switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) {
case 0:
animation.type = @"rippleEffect";
//animation.type = @"cube"; //立方體翻轉
//animation.type = @"oglFlip"; //層翻轉
//animation.type = @"cameraIrisHollowClose";
//animation.type = @"cameraIrisHollowOpen";
break;
case 1:
animation.type = @"pageCurl";
break;
case 2:
animation.type = @"pageUnCurl";
break;
case 3:
animation.type = @"suckEffect";
break;
default:
break;
}
switch (direction) { //方向
case 0:
animation.subtype = kCATransitionFromRight;
break;
case 1:
animation.subtype = kCATransitionFromTop;
break;
case 2:
animation.subtype = kCATransitionFromLeft;
break;
case 3:
animation.subtype = kCATransitionFromBottom;
break;
default:
break;
}
//執行動畫
UIView *bgView = [self.view viewWithTag:150];
NSInteger front = [[bgView subviews] indexOfObject:[bgView viewWithTag:151]];
NSInteger back = [[bgView subviews] indexOfObject:[bgView viewWithTag:152]];
if (someThing) {
[bgView exchangeSubviewAtIndex:front withSubviewAtIndex:back]; //圖形變換
}else {
[bgView exchangeSubviewAtIndex:back withSubviewAtIndex:front];
}
[[bgView layer] addAnimation:animation forKey:@"animation"]; //bgView層執行動畫
//[[self.view layer] addAnimation:animation forKey:@"animation"]; //self.view層執行動畫
}
CABasicAnimation
CABasicAnimation:為層的屬性提供了簡單的插值
//需要改變的屬性
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"cornerRadius"];
animation.fromValue=[NSNumber numberWithFloat:0.0f];
animation.toValue=[NSNumber numberWithFloat:40.0f];
//執行時間
animation.duration = 10.0;
//執行次數
animation.repeatCount=2;
[layer addAnimation:animation forKey:@"change"];
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//角度轉弧度
rotationAnimation.toValue = [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(160))];
rotationAnimation.duration = 1.0f;
//回退動畫
rotationAnimation.autoreverses = YES;
//動畫開始結束的快慢,設定為加速
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[pinLayer addAnimation:rotationAnimation forKey:@"revItUpAnimation"];
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"];
- [UIView beginAnimations:@"zoom out" context:nil];
- [UIView setAnimationDuration:1.f];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
- 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;
- }
CAPropertyAnimation
CAPropertyAnimation:它是CAAnimation的一個抽象子類,支援層在動畫期間為層提供key path。
Core Animation學習筆記( 轉)