对勾提示动画能胜任很多提示场合的任务,比如网页加载完成、登录成功等等。这类动画各个大牛都有很好的demo封装,不过能自己掌握其原理并能自己根据情况封装一个那当然是最好的了。
下边是我用贝塞尔类画的对勾动画
动画设计比较简单,下边我贴出了整个View的代码……
#import "SuccessView.h"
@implementation SuccessView
{
UIView *_logoView;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self drawSuccessLine];
}
return self;
}
- (void)drawSuccessLine{
[_logoView removeFromSuperview];
_logoView = [[UIView alloc] initWithFrame:self.frame];
//曲线建立开始点和结束点
//1. 曲线的中心
//2. 曲线半径
//3. 开始角度
//4. 结束角度
//5. 顺/逆时针方向
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x, self.center.y) radius:self.frame.size.width/2.0 startAngle:0 endAngle:M_PI*2 clockwise:YES];
//对拐角和中点处理
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
//对号第一部分直线的起始
[path moveToPoint:CGPointMake(self.frame.size.width/5, self.frame.size.width/2)];
CGPoint p1 = CGPointMake(self.frame.size.width/5.0*2, self.frame.size.width/4.0*3);
[path addLineToPoint:p1];
//对号第二部分起始
CGPoint p2 = CGPointMake(self.frame.size.width/8.0*7, self.frame.size.width/4.0+8);
[path addLineToPoint:p2];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
//内部填充颜色
layer.fillColor = [UIColor clearColor].CGColor;
//线条颜色
layer.strokeColor = [UIColor orangeColor].CGColor;
//线条宽度
layer.lineWidth = 1;
layer.path = path.CGPath;
//动画设置
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
animation.fromValue = @0;
animation.toValue = @1;
animation.duration = 2;
[layer addAnimation:animation forKey:NSStringFromSelector(@selector(strokeEnd))];
[_logoView.layer addSublayer:layer];
[self addSubview:_logoView];
}
@end
在需要用地方直接创建一个对象就可以了
- (void)showSuccess{
SuccessView *suc = [[SuccessView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
suc.center = self.view.center;
[self.view addSubview:suc];
}