沒啥知識點說的埋哟,就是利用UIBezierPath和CAShapeLayer劃線渤愁,然后動畫懂更,直接貼代碼。
@implementation LQYSuccessView
{
UIView * _logoView;
}
- (instancetype)initWithFrame:(CGRect)frame withState:(BOOL)state
{
self = [super initWithFrame:frame];
if (self) {
if (state) {
[self drawSuccessLine];
}else
{
[self drawFailLine];
}
}
return self;
}
對勾方法
-(void)drawSuccessLine
{
[_logoView removeFromSuperview];
_logoView=[[UIView alloc]initWithFrame:self.frame];
//曲線建立開始點和結束點
//1. 曲線的中心
//2. 曲線半徑
//3. 開始角度
//4. 結束角度
//5. 順/逆時針方向
UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.centerX, self.centerY) radius:self.frame.size.width/2 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 pl =CGPointMake(self.frame.size.width/5*2, self.frame.size.width/4*3);
[path addLineToPoint:pl];
//對號第二部分起始
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 colorWithHexString:@"60b0ff"].CGColor;
//線條寬度
layer.lineWidth = 5;
layer.path = path.CGPath;
//動畫設置
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
animation.fromValue = @0;
animation.toValue = @1;
animation.duration = 1;
[layer addAnimation:animation forKey:NSStringFromSelector(@selector(strokeEnd))];
[_logoView.layer addSublayer:layer];
[self addSubview:_logoView];
}
叉號方法
叉號和對號的區(qū)別就是調整一下會吐的起點和結束點
-(void)drawFailLine
{
[_logoView removeFromSuperview];
_logoView=[[UIView alloc]initWithFrame:self.frame];
//曲線建立開始點和結束點
//1. 曲線的中心
//2. 曲線半徑
//3. 開始角度
//4. 結束角度
//5. 順/逆時針方向
UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.centerX, self.centerY) radius:self.frame.size.width/2 startAngle:0 endAngle:M_PI*2 clockwise:YES];
//對拐角和中心處理
path.lineCapStyle=kCGLineCapRound;
path.lineJoinStyle=kCGLineCapRound;
//對號第一部分的起始
[path moveToPoint:CGPointMake(self.frame.size.width/4, self.frame.size.width/4)];
CGPoint pl =CGPointMake(self.frame.size.width/4*3, self.frame.size.width/4*3);
[path addLineToPoint:pl];
//對號第二部分起始
[path moveToPoint:CGPointMake(self.frame.size.width/4*3, self.frame.size.width/4)];
CGPoint p2 = CGPointMake(self.frame.size.width/4.0, self.frame.size.width/4*3);
[path addLineToPoint:p2];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
//內部填充顏色
layer.fillColor = [UIColor clearColor].CGColor;
//線條顏色
layer.strokeColor = [UIColor colorWithHexString:@"60b0ff"].CGColor;
//線條寬度
layer.lineWidth = 5;
layer.path = path.CGPath;
//動畫設置
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
animation.fromValue = @0;
animation.toValue = @1;
animation.duration = 1;
[layer addAnimation:animation forKey:NSStringFromSelector(@selector(strokeEnd))];
[_logoView.layer addSublayer:layer];
[self addSubview:_logoView];
}
代碼里用了YYKit 里面的屬性推盛,如果你項目里沒有YYKit,修改一下self.centerX和self.centerX就行 谦铃。
具體項目地址請點擊demo