iOS动画解析之圆球加载动画XLBallLoading的实现

2020-01-21 02:22:32王旭

第三段:从C点返回原点

XLBallLoading,ios,加载动画,网络加载等待动画,加载等待动画

三、实现代码

1、第一段运动:

确定起始点、圆心、半径,让蓝色小球绕大圆


 //动画容器的宽度
 CGFloat width = _ballContainer.bounds.size.width;
 //小圆半径
 CGFloat r = (_ball1.bounds.size.width)*ballScale/2.0f;
 //大圆半径
 CGFloat R = (width/2 + r)/2.0;
 UIBezierPath *path1 = [UIBezierPath bezierPath];
 //设置起始位置
 [path1 moveToPoint:_ball1.center];
 //画大圆(第一段的运动轨迹)
 [path1 addArcWithCenter:CGPointMake(R + r, width/2) radius:R startAngle:M_PI endAngle:M_PI*2 clockwise:NO];

2、第二段运动

以灰色小球中心为圆心,以其直径为半径绕小圆,并拼接两段曲线


//画小圆
 UIBezierPath *path1_1 = [UIBezierPath bezierPath];
 //圆心为灰色小球的中心 半径为灰色小球的半径
 [path1_1 addArcWithCenter:CGPointMake(width/2, width/2) radius:r*2 startAngle:M_PI*2 endAngle:M_PI clockwise:NO];
 [path1 appendPath:path1_1];

3、第三段运动


//回到原处
 [path1 addLineToPoint:_ball1.center];

4、位移动画

利用关键帧动画实现小球沿设置好的贝塞尔曲线移动


 //执行动画
 CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 animation1.path = path1.CGPath;
 animation1.removedOnCompletion = YES;
 animation1.duration = [self animationDuration];
 animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 [_ball1.layer addAnimation:animation1 forKey:@"animation1"];

5、缩放动画

在每次位移动画开始时执行缩放动画


-(void)animationDidStart:(CAAnimation *)anim{

 CGFloat delay = 0.3f;
 CGFloat duration = [self animationDuration]/2 - delay;

 [UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseOut| UIViewAnimationOptionBeginFromCurrentState animations:^{
  _ball1.transform = CGAffineTransformMakeScale(ballScale, ballScale);
  _ball2.transform = CGAffineTransformMakeScale(ballScale, ballScale);
  _ball3.transform = CGAffineTransformMakeScale(ballScale, ballScale);
 } completion:^(BOOL finished) {
  [UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseInOut| UIViewAnimationOptionBeginFromCurrentState animations:^{
   _ball1.transform = CGAffineTransformIdentity;
   _ball2.transform = CGAffineTransformIdentity;
   _ball3.transform = CGAffineTransformIdentity;
  } completion:nil];
 }];
}

6、动画循环

在每次动画结束时从新执行动画