iOS 封装打勾和打叉状态动画

没啥知识点说的,就是利用UIBezierPath和CAShapeLayer划线,然后动画,直接贴代码。

@implementation LQYSuccessView

{

UIView* _logoView;

}

- (instancetype)initWithFrame:(CGRect)frame withState:(BOOL)state

{

self= [superinitWithFrame:frame];

if(self) {

if(state) {

[selfdrawSuccessLine];

}else

{

[selfdrawFailLine];

}

}

returnself;

}

-(void)drawSuccessLine

{

[_logoViewremoveFromSuperview];

_logoView=[[UIViewalloc]initWithFrame:self.frame];

//曲线建立开始点和结束点

//1.曲线的中心

//2.曲线半径

//3.开始角度

//4.结束角度

//5.顺/逆时针方向

UIBezierPath* path=[UIBezierPathbezierPathWithArcCenter:CGPointMake(self.centerX,self.centerY)radius:self.frame.size.width/2startAngle:0endAngle:M_PI*2clockwise:YES];

//对拐角和中心处理

path.lineCapStyle=kCGLineCapRound;

path.lineJoinStyle=kCGLineCapRound;

//对号第一部分的起始

[pathmoveToPoint:CGPointMake(self.frame.size.width/5,self.frame.size.width/2)];

CGPointpl =CGPointMake(self.frame.size.width/5*2,self.frame.size.width/4*3);

[pathaddLineToPoint:pl];

//对号第二部分起始

CGPointp2 =CGPointMake(self.frame.size.width/8.0*7,self.frame.size.width/4.0+8);

[pathaddLineToPoint:p2];

CAShapeLayer*layer = [[CAShapeLayeralloc]init];

//内部填充颜色

layer.fillColor= [UIColorclearColor].CGColor;

//线条颜色

layer.strokeColor= [UIColororangeColor].CGColor;

//线条宽度

layer.lineWidth=5;

layer.path= path.CGPath;

//动画设置

CABasicAnimation*animation = [CABasicAnimationanimationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];

animation.fromValue=@0;

animation.toValue=@1;

animation.duration=1;

[layeraddAnimation:animationforKey:NSStringFromSelector(@selector(strokeEnd))];

[_logoView.layeraddSublayer:layer];

[selfaddSubview:_logoView];

}

-(void)drawFailLine

{

[_logoViewremoveFromSuperview];

_logoView=[[UIViewalloc]initWithFrame:self.frame];

//曲线建立开始点和结束点

//1.曲线的中心

//2.曲线半径

//3.开始角度

//4.结束角度

//5.顺/逆时针方向

UIBezierPath* path=[UIBezierPathbezierPathWithArcCenter:CGPointMake(self.centerX,self.centerY)radius:self.frame.size.width/2startAngle:0endAngle:M_PI*2clockwise:YES];

//对拐角和中心处理

path.lineCapStyle=kCGLineCapRound;

path.lineJoinStyle=kCGLineCapRound;

//对号第一部分的起始

[pathmoveToPoint:CGPointMake(self.frame.size.width/4,self.frame.size.width/4)];

CGPointpl =CGPointMake(self.frame.size.width/4*3,self.frame.size.width/4*3);

[pathaddLineToPoint:pl];

//对号第二部分起始

[pathmoveToPoint:CGPointMake(self.frame.size.width/4*3,self.frame.size.width/4)];

CGPointp2 =CGPointMake(self.frame.size.width/4.0,self.frame.size.width/4*3);

[pathaddLineToPoint:p2];

CAShapeLayer*layer = [[CAShapeLayeralloc]init];

//内部填充颜色

layer.fillColor= [UIColorclearColor].CGColor;

//线条颜色

layer.strokeColor= [UIColororangeColor].CGColor;

//线条宽度

layer.lineWidth=5;

layer.path= path.CGPath;

//动画设置

CABasicAnimation*animation = [CABasicAnimationanimationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];

animation.fromValue=@0;

animation.toValue=@1;

animation.duration=1;

[layeraddAnimation:animationforKey:NSStringFromSelector(@selector(strokeEnd))];

[_logoView.layeraddSublayer:layer];

[selfaddSubview:_logoView];

}


iOS 封装打勾和打叉状态动画_第1张图片


代码里用了YYKit 里面的属性,如果你项目里没有YYKit,修改一下self.centerX和self.centerX就行 。

demo :https://github.com/Liangqianyong/LQYState-Animation

你可能感兴趣的:(iOS 封装打勾和打叉状态动画)