1枕屉、CoreAnimation的結(jié)構(gòu)如下
2澈灼、CoreAnimation類介紹
(1)CAAnimation ---> CoreAnimation的基礎(chǔ)類
兩個(gè)代理方法絮缅,監(jiān)聽動畫的開始和結(jié)束
- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
(2)CABasicAnimation ---> 基本動畫
@property(nullable, strong) id fromValue;
@property(nullable, strong) id toValue;
@property(nullable, strong) id byValue;
①實(shí)現(xiàn)大小怎燥、角度负饲、位置的變化通過transform來實(shí)現(xiàn):
CATransform3D transform = CATransform3D + (Make)+ Translate堤魁、Scale、Rotation
animation.toValue = [NSValue valueWithCATransform3D:transform];
//旋轉(zhuǎn)
- (void)rotation
{
//實(shí)現(xiàn)180度的無限旋轉(zhuǎn)
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
anim.byValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 0, 1)];
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.myView.layer addAnimation:anim forKey:nil];
}
//位置
- (void)positon
{
//實(shí)現(xiàn)位置的移動
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D form = CATransform3DMakeTranslation(350, 350, 0);
anim.toValue = [NSValue valueWithCATransform3D:form];
anim.duration = 1;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.myView.layer addAnimation:anim forKey:nil];
}
//大小
- (void)scale
{
//實(shí)現(xiàn)大小的變化
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform";
CATransform3D form = CATransform3DMakeScale(1.2, 1.2, 1);
anim.toValue = [NSValue valueWithCATransform3D:form];
anim.duration = 0.5;
anim.repeatCount = MAXFLOAT;
anim.autoreverses = YES;
[self.myView.layer addAnimation:anim forKey:nil];
}
②實(shí)現(xiàn)大小、位置的變化通過bounds、position來實(shí)現(xiàn):
//大小
- (void)scale
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
anim.duration = 2;
anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)];
[_myView.layer addAnimation:anim forKey:nil];
}
//位置
- (void)positon
{
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"position";
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 500)];
// 取消反彈
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[_myView.layer addAnimation:anim forKey:nil];
}
(3)CAKeyframeAnimation --->關(guān)鍵幀動畫
@property(nullable, copy) NSArray *values;
@property(nullable, copy) NSArray<NSNumber *> *keyTimes;
@property(nullable) CGPathRef path;
//位置1
- (void)position1
{
//多步動畫
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position.x";
animation.values = @[ @0, @10, @-10, @10, @0 ];
//設(shè)置每一幀執(zhí)行的時(shí)間長短 這個(gè)的取值為0-1卤唉,代表占用時(shí)間的比例
animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
animation.duration = 0.4;
//設(shè)置幀的中間值如何計(jì)算
animation.calculationMode = kCAAnimationPaced;
//這個(gè)屬性確定動畫執(zhí)行的狀態(tài)是否疊加在控件的原狀態(tài)上
//默認(rèn)設(shè)置為NO添履,如果我們執(zhí)行兩次位置移動的動畫积仗,會從同一位置執(zhí)行兩次
//如果設(shè)置為YES,則會在第一次執(zhí)行的基礎(chǔ)上執(zhí)行第二次動畫
animation.additive = YES;
//如果設(shè)置為YES,代表動畫每次重復(fù)執(zhí)行的效果會跟上一次相反
animation.autoreverses = YES;
[self.myView.layer addAnimation:animation forKey:@"position1"];
self.myView.layer.zPosition = 1;
}
//位置2
- (void)position2
{
//沿貝塞爾曲線的動畫
// 這個(gè)方法將創(chuàng)建橢圓形路徑
CGRect boundingRect = CGRectMake(0, 0, 300, 300);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:boundingRect cornerRadius:150];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
//設(shè)置一個(gè)路徑
animation.path = path.CGPath;
animation.duration = 4;
animation.additive = YES;
animation.repeatCount = HUGE_VALF;
animation.calculationMode = kCAAnimationPaced;
animation.rotationMode = kCAAnimationRotateAuto;
[self.myView.layer addAnimation:animation forKey:nil];
}
(4)CASpringAnimation ---> 彈性動畫
-(void)springAnimation{
CASpringAnimation *animation = [CASpringAnimation animationWithKeyPath:@"position"];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(175, 400)];
//質(zhì)量,影響圖層運(yùn)動時(shí)的彈簧慣性,質(zhì)量越大刽沾,彈簧拉伸和壓縮的幅度越大
//如果把質(zhì)量改成10,則動畫變成 動畫的速度變慢排拷,并且波動幅度變大
animation.mass = 1;
//阻尼系數(shù)侧漓,阻止彈簧伸縮的系數(shù),阻尼系數(shù)越大监氢,停止越快
animation.damping = 30;
//初始速率 動畫視圖的初始速度大小
animation.initialVelocity = 10;
//剛度 剛度系數(shù)越大布蔗,形變產(chǎn)生的力就越大,運(yùn)動越快
animation.stiffness = 10;
animation.duration = animation.settlingDuration;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[self.myView.layer addAnimation:animation forKey:@"springAnimation"];
}
(5)CATransition ---> 轉(zhuǎn)場動畫
@property(copy) NSString *type; 動畫過渡類型 ----->
fade',
moveIn',push' and
reveal'. Defaults tofade'. @property(nullable, copy) NSString *subtype; 動畫過渡方向 ----->
fromLeft',fromRight',
fromTop' and * `fromBottom'
- (void)transition
{
CATransition * ani = [CATransition animation];
ani.type = @"pageCurl";
ani.subtype = kCATransitionFromRight;
[self.myView.layer addAnimation:ani forKey:@""];
}
(5)CAAnimationGroup ---> 動畫組
@property(nullable, copy) NSArray<CAAnimation *> *animations;
3浪腐、動畫暫停和重啟
//暫停
-(void)pauseAnimation
{
CFTimeInterval pausedTime = [self.myView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
// 讓CALayer的時(shí)間停止走動
self.myView.layer.speed = 0.0;
// 讓CALayer的時(shí)間停留在pausedTime這個(gè)時(shí)刻
self.myView.layer.timeOffset = pausedTime;
}
//重啟
-(void)resumeAnimation
{
CFTimeInterval pausedTime = self.myView.layer.timeOffset;
// 1. 讓CALayer的時(shí)間繼續(xù)行走
self.myView.layer.speed = 1.0;
// 2. 取消上次記錄的停留時(shí)刻
self.myView.layer.timeOffset = 0.0;
// 3. 取消上次設(shè)置的時(shí)間
self.myView.layer.beginTime = 0.0;
// 4. 計(jì)算暫停的時(shí)間(這里也可以用CACurrentMediaTime()-pausedTime)
CFTimeInterval timeSincePause = [self.myView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
// 5. 設(shè)置相對于父坐標(biāo)系的開始時(shí)間(往后退timeSincePause)
self.myView.layer.beginTime = timeSincePause;
}