//? CoreAnimationDemo
// ???CATransaction事務(wù)動(dòng)畫(顯示動(dòng)畫(開頭和結(jié)尾)和隱式動(dòng)畫)
// ???CAAnimation是一個(gè)基類定義一些動(dòng)畫的基本屬性和方法
// ???CAPropertyAnimation屬相動(dòng)畫是一個(gè)抽象的子類,支持動(dòng)畫的顯示圖層的關(guān)鍵路徑(KeyPath)中定制的屬性
// ???CABasicAnimation基礎(chǔ)動(dòng)畫簡(jiǎn)單的為圖層屬性提供修改
// ???CAAnimationGroup組動(dòng)畫
// ???CAKeyframeAnimation關(guān)鍵幀動(dòng)畫
// ???CATransition過渡動(dòng)畫
//動(dòng)畫結(jié)束不移除動(dòng)畫效果(不返回)
CAAnimation *caa = nil;
caa.removedOnCompletion = NO;
caa.fillMode = kCAFillModeForwards;
CABasicAnimation *base;
//在你原來的基礎(chǔ)上繼續(xù)動(dòng)畫
base.cumulative =YES;
//事務(wù)動(dòng)畫
- (void)doTranscationAnimation {
//事務(wù)分為顯示和隱式
//隱式事務(wù)指的是我們不去控制它的發(fā)生過程,只關(guān)心結(jié)果的時(shí)候,系統(tǒng)默認(rèn)幫我們添加的一個(gè)動(dòng)畫的過程
//顯示事務(wù)指的是我們明確的控制動(dòng)畫的發(fā)生過程
//顯示動(dòng)畫
//開始
[CATransaction begin];
//動(dòng)畫時(shí)間
[CATransaction setAnimationDuration:0.5];
//速率函數(shù)
[CATransactionsetAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
//動(dòng)畫(圖層)動(dòng)畫體
if(_layer.cornerRadius >= 30) {
_layer.cornerRadius = 10;
_layer.backgroundColor = [UIColor orangeColor].CGColor;
}else{
_layer.cornerRadius = 50;
_layer.backgroundColor= [UIColor blueColor].CGColor;
}
//提交
[CATransaction commit];
}
/*
keyPath::::::::::
邊框顏色:borderColor
旋轉(zhuǎn):transform.rotation.ztransform.rotation.z
縮放:transform.scale
位置:position
背景顏色:backgroundColor
*/
//基礎(chǔ)動(dòng)畫
- (void)doBaseAnimation {
// ???CABasicAnimation
//創(chuàng)建基礎(chǔ)動(dòng)畫對(duì)象
CABasicAnimation *borederColorAnimation = [CABasicAnimation animation];
//設(shè)置關(guān)鍵路徑(做什么動(dòng)畫)
borederColorAnimation.keyPath = @"borderColor";
//動(dòng)畫時(shí)間
borederColorAnimation.duration= 0.5f;
//動(dòng)畫的值
borederColorAnimation.byValue= (id)[UIColor blueColor].CGColor;
//是否自動(dòng)重復(fù)
borederColorAnimation.autoreverses =YES;
//設(shè)置重復(fù)次數(shù)
borederColorAnimation.repeatCount= 5;
//把動(dòng)畫添加到動(dòng)畫對(duì)象上
// ???[_layer addAnimation:borederColorAnimation forKey:@"borederColor_Animation"];
//旋轉(zhuǎn)
CABasicAnimation *rotationAnimotion = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimotion.duration= 1.5;
rotationAnimotion.toValue= @(M_PI);
rotationAnimotion.autoreverses=YES;
// ???[_layer addAnimation:rotationAnimotion forKey:@"rotation_Animotion"];
//縮放
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration= 1.5;
scaleAnimation.toValue= @(2.0);
scaleAnimation.autoreverses=YES;
// ???[_layer addAnimation:scaleAnimation forKey:@"scale_Animation”];
//組動(dòng)畫
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations= @[borederColorAnimation,rotationAnimotion,scaleAnimation];
group.duration=3;
[_layer addAnimation:groupforKey:@"Group_Animaton"];
}
//關(guān)鍵幀動(dòng)畫
- (void)doKeyFrameAnimation {
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.duration= 2;
//動(dòng)畫過程的設(shè)置
//獲得原始數(shù)據(jù)獲取圖層位置
CGPointpoint =_layer.position;
//設(shè)置過程
positionAnimation.values= @[[NSValuevalueWithCGPoint:point],[NSValuevalueWithCGPoint:CGPointMake(0, 400)],[NSValuevalueWithCGPoint:CGPointMake(300, 400)],[NSValuevalueWithCGPoint:point]];
//規(guī)定每個(gè)幀動(dòng)畫的時(shí)間從什么時(shí)候開始
positionAnimation.keyTimes= @[@0,@0.3,@0.5,@1];
//將動(dòng)畫添加個(gè)目標(biāo)對(duì)象
[_layer addAnimation:position AnimationforKey:@"position_Animation"];
CAKeyframeAnimation *backgroundColorAnimation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
backgroundColorAnimation.duration= 2;
backgroundColorAnimation.values= @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];
[_layer addAnimation:backgroundColor AnimationforKey:@"backgroundColor_Animation"];
}
//過渡動(dòng)畫
- (void)doTransitionAnimation {
CATransition *transitionAnimation = [CATransition animation];
transitionAnimation.duration= 2;
//kCATransitionMoveIn
transitionAnimation.type=@"cube";
NSArray *subtypeArray = @[kCATransitionFromLeft,kCATransitionFromRight,kCATransitionFromBottom,kCATransitionFromTop,kCATransitionFromLeft];
transitionAnimation.subtype= subtypeArray[arc4random()%(subtypeArray.count)];
[_layer addAnimation:transition AnimationforKey:@"transition_Animation"];
/*
//transitionAnimation.type = @"oglFlip";
以下API效果可以安全使用
cube方塊
suckEffect三角
rippleEffect水波抖動(dòng)
pageCurl上翻頁(yè)
pageUnCurl下翻頁(yè)
oglFlip上下翻轉(zhuǎn)
cameraIrisHollowOpen鏡頭快門開
cameraIrisHollowClose鏡頭快門開
以下API效果請(qǐng)慎用
spewEffect新版面在屏幕下方中間位置被釋放出來覆蓋舊版面.
genieEffect舊版面在屏幕左下方或右下方被吸走,顯示出下面的新版面
unGenieEffect新版面在屏幕左下方或右下方被釋放出來覆蓋舊版面.
twist版面以水平方向像龍卷風(fēng)式轉(zhuǎn)出來.
tubey版面垂直附有彈性的轉(zhuǎn)出來.
swirl舊版面360度旋轉(zhuǎn)并淡出,顯示出新版面.
charminUltra舊版面淡出并顯示新版面.
zoomyIn新版面由小放大走到前面,舊版面放大由前面消失.
zoomyOut新版面屏幕外面縮放出現(xiàn),舊版面縮小消失.
oglApplicationSuspend像按”home”按鈕的效果.
*/
}
結(jié)構(gòu)字段的鍵的取值
CAAnimation供支持使用關(guān)鍵路徑訪問選擇的結(jié)構(gòu)字段。這在為動(dòng)畫關(guān)鍵路徑指定結(jié)構(gòu)字段的時(shí)候非常有幫助,同時(shí)你可以使用setValue:forKeyPath:和valueForKeyPath來設(shè)置和讀取相應(yīng)的值撬码。
CATransform3D公開如下的字段:
結(jié)構(gòu)體字段???????????????? ? ? ? ?? 描述
transform.rotation.x????????????? The rotation, in radians, in the x axis.
transform.rotation.y????????????? The rotation, in radians, in the y axis.
transform.rotation.z??????????? The rotation, in radians, in the z axis.
transform.rotation????????????????? The rotation, in radians, in the z axis. This is identical to setting the rotation.z field.
transform.scale.x????????????????? Scale factor for the x axis.
transform.scale.y????????????????? Scale factor for the y axis.
transform.scale.z???????????????? Scale factor for the z axis.
transform.scale?????????????????? Average of all three scale factors.
transform.translation.x????????? Translate in the x axis.
transform.translation.y?????????? Translate in the y axis.
translation.z????????????????????????? Translate in the z axis.
translation??????????????????????????? Translate in the x and y axis. Value is an NSSize or CGSize.
CGPoint公開如下字段:
結(jié)構(gòu)體字段描述
x????????????????????????? The x component of the point.
y?????????????????????????? The y component of the point.
CGSize公開如下字段:
結(jié)構(gòu)體字段描述
width????????????????????? The width component of the size.
height???????????????????? The height component of the size.
CGRect公開如下字段:
結(jié)構(gòu)體字段描述
origin????????????????????? The origin of the rectangle as a CGPoint.
origin.x??????????????????? The x component of the rectangle origin.
origin.y???????????????????? The y component of the rectangle origin.
size?????????????????????????? The size of the rectangle as a CGSize.
size.width????????????????? The width component of the rectangle size.
size.height??????????????? The height component of the rectangle size
你不可以通過Objective-C 2.0的屬性方法來指定一個(gè)結(jié)構(gòu)字段的關(guān)鍵路徑儿倒。如下的代碼是無法正常執(zhí)行的:
myLayer.transform.rotation.x = 0;
相反你必須使用setValue:forKeyPath:或者valuForKeyPath:,如下:
[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];