CoreAnimation加载动画功能,如我们平时上网页在加载时出现的三个圆在加载的动画效果。该功能用到的主要方法有:NSTimer定时器、对视图进行设置平移和缩放、创建UIBezierPath基于矢量的路径添加画弧方法、关键帧动画CAKeyframeAnimation设置相关属性。
三个圆形加载画面,主要由三个view组成,分别为左中右视图,设置的核心代码为:
//1.初始化centerCir视图
_centerCir = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Radius, Radius)];
_centerCir.center = self.view.center;
_centerCir.layer.cornerRadius = Radius * 0.5;
_centerCir.layer.masksToBounds = YES;
_centerCir.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_centerCir];
//2.初始化leftCir视图
_leftCir = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Radius, Radius)];
_leftCir.center = CGPointMake(_centerCir.center.x - Radius, _centerCir.center.y);
_leftCir.layer.cornerRadius = Radius * 0.5;
_leftCir.layer.masksToBounds = YES;
_leftCir.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_leftCir];
//3.初始化rightCir视图
_rightCir = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Radius, Radius)];
_rightCir.center = CGPointMake(_centerCir.center.x + Radius, _centerCir.center.y);
_rightCir.layer.cornerRadius = Radius * 0.5;
_rightCir.layer.masksToBounds = YES;
_rightCir.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_rightCir];
定时器设置核心代码:
//1.初始化定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(timerAction:)
userInfo:nil
repeats:YES];
[_timer fire];
定时器响应时间核心代码:
//设置动画
[UIView animateWithDuration:1
animations:^{
//view以最初位置的中心点进行平移
_leftCir.transform = CGAffineTransformMakeTranslation(-Radius, 0);
//view以闯入的状态进行缩放0.75倍
_leftCir.transform = CGAffineTransformScale(_leftCir.transform, 0.75, 0.75);
_rightCir.transform = CGAffineTransformMakeTranslation(Radius, 0);
_rightCir.transform = CGAffineTransformScale(_rightCir.transform, 0.75, 0.75);
_centerCir.transform = CGAffineTransformScale(_centerCir.transform, 0.75, 0.75);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:1
animations:^{
//设置view的状态为最初状态
_leftCir.transform = CGAffineTransformIdentity;
_centerCir.transform = CGAffineTransformIdentity;
_rightCir.transform = CGAffineTransformIdentity;
[self againAnimation];
}];
}];
againAnimation核心代码:
//创建基于矢量的路径,bezierPath可实现任意类型
UIBezierPath *leftCirPath = [UIBezierPath bezierPath];
//画弧方法
[leftCirPath addArcWithCenter:self.view.center //弧线中心点坐标
radius:Radius //弧线坐在圆的半径
startAngle:M_PI //弧线开始的角度值, M_PI=180°
endAngle:2 * M_PI + 2 * M_PI //弧线结束的角度值
clockwise:NO]; //是否顺时针
//定义关键帧动画
CAKeyframeAnimation *leftCirAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//动画路径
leftCirAnimation.path = leftCirPath.CGPath;
//速度控制函数,控制动画运行节奏
/**
kCAMediaTimingFunctionLinear: 线性,均速
kCAMediaTimingFunctionEaseIn: 渐进,动画缓慢进入,加速离开
kCAMediaTimingFunctionEaseOut: 渐出,动画全速进入,减速到达目的地
kCAMediaTimingFunctionEaseInEaseOut: 渐进渐出,缓慢进入,中间加速,减速到达目的地,默认
*/
leftCirAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//动画结束,保持动画最后的状态
leftCirAnimation.fillMode = kCAFillModeForwards;
//保持显示动画执行后的状态
leftCirAnimation.removedOnCompletion = YES;
//动画重复次数
leftCirAnimation.repeatCount = 2;
//动画持续时间
leftCirAnimation.duration = 1;
[_leftCir.layer addAnimation:leftCirAnimation forKey:@"cc"];
UIBezierPath * rightCirPath = [UIBezierPath bezierPath];
[rightCirPath addArcWithCenter:self.view.center
radius:Radius
startAngle:0
endAngle:M_PI + 2 * M_PI
clockwise:NO];
CAKeyframeAnimation *rightCirAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
rightCirAnimation.path = rightCirPath.CGPath;
rightCirAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
rightCirAnimation.fillMode = kCAFillModeForwards;
rightCirAnimation.removedOnCompletion = YES;
rightCirAnimation.repeatCount = 2;
rightCirAnimation.duration = 1;
[_rightCir.layer addAnimation:rightCirAnimation forKey:@"hh"];