iOS動畫(二)CoreAnimation

1、CoreAnimation的結(jié)構(gòu)如下

1.png

2、CoreAnimation類介紹

(1)CAAnimation ---> CoreAnimation的基礎(chǔ)類

兩個代理方法,監(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;

①實現(xiàn)大小、角度、位置的變化通過transform來實現(xiàn):

CATransform3D transform = CATransform3D + (Make)+ Translate、Scale、Rotation
animation.toValue = [NSValue valueWithCATransform3D:transform];

//旋轉(zhuǎn)
- (void)rotation
{
  //實現(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
{
  //實現(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
{
  //實現(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];
}
②實現(xiàn)大小、位置的變化通過bounds、position來實現(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í)行的時間長短 這個的取值為0-1,代表占用時間的比例
    animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
    animation.duration = 0.4;
    //設(shè)置幀的中間值如何計算
    animation.calculationMode = kCAAnimationPaced;
    //這個屬性確定動畫執(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
{
    //沿貝塞爾曲線的動畫

    // 這個方法將創(chuàng)建橢圓形路徑
    CGRect boundingRect = CGRectMake(0, 0, 300, 300);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:boundingRect cornerRadius:150];

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    //設(shè)置一個路徑    
    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ì)量,影響圖層運動時的彈簧慣性,質(zhì)量越大,彈簧拉伸和壓縮的幅度越大
    //如果把質(zhì)量改成10,則動畫變成 動畫的速度變慢,并且波動幅度變大
    animation.mass = 1;
    //阻尼系數(shù),阻止彈簧伸縮的系數(shù),阻尼系數(shù)越大,停止越快
    animation.damping = 30;
    //初始速率 動畫視圖的初始速度大小
    animation.initialVelocity = 10;
    //剛度 剛度系數(shù)越大,形變產(chǎ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' andreveal'. Defaults to fade'. @property(nullable, copy) NSString *subtype; 動畫過渡方向 ----->fromLeft', fromRight',fromTop' and * `fromBottom'

23.png
- (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的時間停止走動
    self.myView.layer.speed = 0.0;
    // 讓CALayer的時間停留在pausedTime這個時刻
    self.myView.layer.timeOffset = pausedTime;
}
//重啟
-(void)resumeAnimation
{
    CFTimeInterval pausedTime = self.myView.layer.timeOffset;
    // 1. 讓CALayer的時間繼續(xù)行走
    self.myView.layer.speed = 1.0;
    // 2. 取消上次記錄的停留時刻
    self.myView.layer.timeOffset = 0.0;
    // 3. 取消上次設(shè)置的時間
    self.myView.layer.beginTime = 0.0;
    // 4. 計算暫停的時間(這里也可以用CACurrentMediaTime()-pausedTime)
    CFTimeInterval timeSincePause = [self.myView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    // 5. 設(shè)置相對于父坐標(biāo)系的開始時間(往后退timeSincePause)
    self.myView.layer.beginTime = timeSincePause;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容