在一个View上绘制一条直线 然后做出相应的动画效果 可以这样封装三个方法:
/**
* 划线工具
*
* @param lineArray 线段的点数组 NSValue 类型 默认第一个点为起点
* @param time 划线时间
* @param strokeColor 线段颜色
*/
- (void)axcBaseAniamtionWithLinePointArray:(NSArray <NSValue *> *)lineArray time:(CGFloat )time strokeColor:(UIColor *)strokeColor{
if (lineArray.count <= 1) {
if (_axcBasePrintLog) {
NSLog(@"%@: 你的数组对象少于一个,无法划线!",self);
}
return;
}
if (_axcBasePrintLog) {
NSLog(@"%@: 当前正在划线 方法名:axcBaseAniamtionWithLinePointArray 起点:%@ 划线时间:%.2f,颜色:%@ ",self,NSStringFromCGPoint([lineArray[0] CGPointValue]),time,strokeColor);
}
UIBezierPath *_path=[UIBezierPath bezierPath];
CGPoint point1 = [lineArray[0] CGPointValue];
[_path moveToPoint:point1];
for (int i = 1; i < lineArray.count; i ++) {
[_path addLineToPoint: [lineArray[i] CGPointValue]];
if (_axcBasePrintLog) {
NSLog(@"%@:遍历划线点(%d):%@ ",self,i + 1,NSStringFromCGPoint([lineArray[i] CGPointValue]));
}
}
CAShapeLayer *shapeLayer=[CAShapeLayer layer];
shapeLayer.path=_path.CGPath;
shapeLayer.fillColor=[UIColor clearColor].CGColor;//填充颜色
shapeLayer.strokeColor=strokeColor.CGColor;//边框颜色
[self.view.layer addSublayer:shapeLayer];
// 动画
CABasicAnimation *pathAniamtion = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
// 时间
pathAniamtion.duration = time;
pathAniamtion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAniamtion.fromValue = [NSNumber numberWithFloat:0.0f];
// 划线段的百分之多少
pathAniamtion.toValue = [NSNumber numberWithFloat:1];
pathAniamtion.autoreverses = NO;
[shapeLayer addAnimation:pathAniamtion forKey:nil];
}
/**
* 圆规工具
*
* @param Center 中心点
* @param radius 半径
* @param startAngle 开始角度
* @param endAngle 结束角度
* @param clockwise 是否顺时针
* @param time 作图时间
* @param strokeColor 颜色
*/
- (void)axcBaseAniamtionArcWithCenter:(CGPoint )Center
radius:(CGFloat )radius
startAngle:(CGFloat )startAngle
endAngle:(CGFloat )endAngle
clockwise:(BOOL )clockwise
time:(CGFloat )time
strokeColor:(UIColor *)strokeColor{
UIBezierPath *_path=[UIBezierPath bezierPath];
[_path addArcWithCenter:Center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];
CAShapeLayer *shapeLayer=[CAShapeLayer layer];
shapeLayer.path=_path.CGPath;
shapeLayer.fillColor=[UIColor clearColor].CGColor;//填充颜色
shapeLayer.strokeColor=strokeColor.CGColor;//边框颜色
[self.view.layer addSublayer:shapeLayer];
// 动画
CABasicAnimation *pathAniamtion = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
// 时间
pathAniamtion.duration = time;
pathAniamtion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAniamtion.fromValue = [NSNumber numberWithFloat:0.0f];
// 划线段的百分之多少
pathAniamtion.toValue = [NSNumber numberWithFloat:1];
pathAniamtion.autoreverses = NO;
[shapeLayer addAnimation:pathAniamtion forKey:nil];
}
/**
* 清除所有划线
*/
- (void)axcBaseClearAllShapeLayer{
if (_axcBasePrintLog) {
NSLog(@"%@: 当前执行清除该界面上的所有划线!",self);
}
NSArray *shapeLayerArray = self.view.layer.sublayers;
for (int i = 0; i < shapeLayerArray.count; i ++) {
if ([shapeLayerArray[i] isKindOfClass:[CAShapeLayer class]]) {
[shapeLayerArray[i] removeFromSuperlayer];
}
}
}