@property (nonatomic, strong) CAShapeLayer *line1; 绘制三角形
@property (nonatomic, strong) UIBezierPath *line1Path;
@property (nonatomic, strong) CAShapeLayer *line2; 绘制过度弧线
@property (nonatomic, strong) UIBezierPath *line2Path;
@property (nonatomic, strong) CAShapeLayer *line3; 左竖线
@property (nonatomic, strong) UIBezierPath *line3Path;
@property (nonatomic, strong) CAShapeLayer *line4; 右竖线
@property (nonatomic, strong) UIBezierPath *line4Path;
- (void)createView{
// 添加三角形
self.line1 = [CAShapeLayer layer];
self.line1.fillColor = [UIColor clearColor].CGColor;
self.line1.strokeColor = [UIColor colorWithRed:0.03 green:0.13 blue:0.96 alpha:1.00].CGColor;
self.line1.lineCap = kCALineCapButt;
self.line1.lineJoin = kCALineJoinRound;
self.line1.lineWidth = 20;
self.line1Path = [[UIBezierPath alloc] init];
[self.line1Path moveToPoint:CGPointMake(50, 100)];
[self.line1Path addLineToPoint:CGPointMake(50, 43)];
[self.line1Path addLineToPoint:CGPointMake(150, 100)];
[self.line1Path addLineToPoint:CGPointMake(50, 157)];
[self.line1Path addLineToPoint:CGPointMake(50, 100)];
[self.line1Path closePath];
self.line1.path = self.line1Path.CGPath;
[self.layer addSublayer:self.line1];
self.line1.strokeEnd = 0;
/// 添加过度圆弧
self.line2 = [CAShapeLayer layer];
self.line2.fillColor = [UIColor clearColor].CGColor;
self.line2.strokeColor = [UIColor colorWithRed:0.03 green:0.13 blue:0.96 alpha:1.00].CGColor;
self.line2.lineCap = kCALineCapRound;
self.line2.lineWidth = 20;
self.line2Path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 110) radius:50 startAngle:M_PI endAngle:0 clockwise:NO];
self.line2.path = self.line2Path.CGPath;
[self.layer addSublayer:self.line2];
self.line2.strokeStart = 1;
添加左边竖杠
self.line3 = [CAShapeLayer layer];
self.line3.fillColor = [UIColor clearColor].CGColor;
self.line3.strokeColor = [UIColor colorWithRed:0.03 green:0.13 blue:0.96 alpha:1.00].CGColor;
self.line3.lineCap = kCALineCapRound;
self.line3.lineWidth = 20;
self.line3Path = [[UIBezierPath alloc] init];
[self.line3Path moveToPoint:CGPointMake(50, 43.0)];
[self.line3Path addLineToPoint:CGPointMake(50, 157)];
self.line3.path = self.line3Path.CGPath;
[self.layer addSublayer:self.line3];
self.line3.strokeEnd = 1;
/// 添加右边竖杠
self.line4 = [CAShapeLayer layer];
self.line4.fillColor = [UIColor clearColor].CGColor;
self.line4.strokeColor = [UIColor colorWithRed:0.03 green:0.13 blue:0.96 alpha:1.00].CGColor;
self.line4.lineCap = kCALineCapRound;
self.line4.lineWidth = 20;
self.line4Path = [[UIBezierPath alloc] init];
[self.line4Path moveToPoint:CGPointMake(150, 157)];
[self.line4Path addLineToPoint:CGPointMake(150, 43)];
self.line4.path = self.line4Path.CGPath;
[self.layer addSublayer:self.line4];
self.line4.strokeEnd = 1;
}
- (void)playAnimation{
/// 右竖线平移
self.line4.path = [self pingyiWithMovePoint:CGPointMake(150, 120) addPoint:CGPointMake(150, 20)].CGPath;
[self.line4 addAnimation:[self pathAnimationWithDuration:0.2] forKey:nil];
/// 左竖线平移
self.line3.path = [self pingyiWithMovePoint:CGPointMake(50, 60) addPoint:CGPointMake(50, 160)].CGPath;
[self.line3 addAnimation:[self pathAnimationWithDuration:0.2] forKey:nil];
----延时0.2s执行-----------
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
// 右直线缩短消失
[self addAnimationStrokeEndWithLayer:self.line4 duration:0.2 from:1 to:0];
});
----延时0.4s执行-----------
dispatch_time_t delayTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime2, dispatch_get_main_queue(), ^{
// 左竖线上移缩短
self.line3.path = [self pingyiWithMovePoint:CGPointMake(50, 43) addPoint:CGPointMake(50, 100)].CGPath;
[self.line3 addAnimation:[self pathAnimationWithDuration:0.2] forKey:nil];
// 弧线动画
[self addAnimationStrokeStartWithLayer:self.line2 duration:0.2 from:1 to:0];
self.line4.hidden = YES;
});
----延时0.5s执行-----------
dispatch_time_t delayTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime1, dispatch_get_main_queue(), ^{
// 弧线动画
[self addAnimationStrokeEndWithLayer:self.line2 duration:0.2 from:1 to:0];
// 绘制三角形
[self addAnimationStrokeEndWithLayer:self.line1 duration:0.6 from:0 to:1];
});
}
- (void)pauseAnimation{
// 绘制三角形
[self addAnimationStrokeEndWithLayer:self.line1 duration:0.6 from:1 to:0];
----延时0.2s执行-----------
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
// 弧线动画
[self addAnimationStrokeEndWithLayer:self.line2 duration:0.2 from:0 to:1];
});
----延时0.3s执行-----------
dispatch_time_t delayTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime1, dispatch_get_main_queue(), ^{
self.line4.hidden = NO;
// 右直线缩短消失
[self addAnimationStrokeEndWithLayer:self.line4 duration:0.2 from:0 to:1];
});
----延时0.4s执行-----------
dispatch_time_t delayTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime2, dispatch_get_main_queue(), ^{
// 弧线动画
[self addAnimationStrokeStartWithLayer:self.line2 duration:0.2 from:0 to:1];
/// 右竖线平移
self.line4.path = [self pingyiWithMovePoint:CGPointMake(150, 120) addPoint:CGPointMake(150, 20)].CGPath;
[self.line4 addAnimation:[self pathAnimationWithDuration:0.2] forKey:nil];
/// 左竖线平移
self.line3.path = [self pingyiWithMovePoint:CGPointMake(50, 60) addPoint:CGPointMake(50, 160)].CGPath;
[self.line3 addAnimation:[self pathAnimationWithDuration:0.2] forKey:nil];
});
----延时0.6s执行-----------
dispatch_time_t delayTime3 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime3, dispatch_get_main_queue(), ^{
/// 左竖线平移
self.line3.path = [self pingyiWithMovePoint:CGPointMake(50, 43) addPoint:CGPointMake(50, 157)].CGPath;
[self.line3 addAnimation:[self pathAnimationWithDuration:0.1] forKey:nil];
/// 右竖线平移
self.line4.path = [self pingyiWithMovePoint:CGPointMake(150, 157) addPoint:CGPointMake(150, 43)].CGPath;
[self.line4 addAnimation:[self pathAnimationWithDuration:0.2] forKey:nil];
});
}
/// 平移动画
- (UIBezierPath *)pingyiWithMovePoint:(CGPoint)movepoint addPoint:(CGPoint)addpoint{
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:movepoint];
[linePath addLineToPoint:addpoint];
return linePath;
}
/// strokeEnd 动画
- (void)addAnimationStrokeEndWithLayer:(CAShapeLayer *)layer duration:(CGFloat)duration from:(CGFloat)from to:(CGFloat)to{
CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
strokeEndAnimation.fillMode = kCAFillModeForwards;
strokeEndAnimation.removedOnCompletion = NO;
strokeEndAnimation.duration = duration;
strokeEndAnimation.fromValue = @(from);
strokeEndAnimation.toValue = @(to);
[layer addAnimation:strokeEndAnimation forKey:nil];
}
/// strokeStart动画
- (void)addAnimationStrokeStartWithLayer:(CAShapeLayer *)layer duration:(CGFloat)duration from:(CGFloat)from to:(CGFloat)to{
CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
strokeEndAnimation.fillMode = kCAFillModeForwards;
strokeEndAnimation.removedOnCompletion = NO;
strokeEndAnimation.duration = duration;
strokeEndAnimation.fromValue = @(from);
strokeEndAnimation.toValue = @(to);
[layer addAnimation:strokeEndAnimation forKey:nil];
}
- (CABasicAnimation *)pathAnimationWithDuration:(CGFloat)duration {
CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnima.duration = duration;
pathAnima.fillMode = kCAFillModeForwards;
pathAnima.removedOnCompletion = NO;
return pathAnima;
}