如有发现问题, 请指正!
@interface ViewController () <CAAnimationDelegate>
@property (nonatomic, strong) CAShapeLayer *shaperLayer;
@end
@implementation ViewController {
CADisplayLink *_displayLink;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadShaperLayer];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self startDisplayLink];
// });
}
- (void)loadShaperLayer {
//创建圆路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 80, 80)];
//矩形
path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 80, 80)];
//圆弧
path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:M_PI clockwise:YES];
//带圆角的矩形
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 200, 200) cornerRadius:30];
//指定圆角
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 200, 200) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 0)];
//自定义
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(200, 100)];
[path addLineToPoint:CGPointMake(200, 200)];
[path addLineToPoint:CGPointMake(100, 200)];
[path addLineToPoint:CGPointMake(100, 300)];
[path addLineToPoint:CGPointMake(200, 300)];
[path addArcWithCenter:CGPointMake(100, 400) radius:100 startAngle:0 endAngle:M_PI clockwise:YES];
[path addLineToPoint:CGPointMake(0, 300)];
[path addLineToPoint:CGPointMake(95, 300)];
[path addLineToPoint:CGPointMake(95, 200)];
[path addLineToPoint:CGPointMake(0, 200)];
[path addLineToPoint:CGPointMake(0, 100)];
[path closePath];
//一把丑剑
#if 1
path = [UIBezierPath bezierPath];
//大小弧
[path addArcWithCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:M_PI clockwise:YES];
[path addLineToPoint:CGPointMake(50, 100)];
[path addArcWithCenter:CGPointMake(100, 100) radius:50 startAngle:M_PI endAngle:M_PI*2 clockwise:NO];
[path addLineToPoint:CGPointMake(200, 100)];
//剑梁
[path moveToPoint:CGPointMake(100, -50)];
[path addLineToPoint:CGPointMake(100, 250)];
//大小圆
[path addArcWithCenter:CGPointMake(100, 250) radius:50 startAngle:M_PI_2 endAngle:(M_PI*2+M_PI_2) clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 320) radius:20 startAngle:-M_PI_2 endAngle:-(M_PI*2+M_PI_2) clockwise:NO];
//剑刃
[path moveToPoint:CGPointMake(95, 340)];
[path addLineToPoint:CGPointMake(95, 640)];
[path addLineToPoint:CGPointMake(100, 660)];
[path addLineToPoint:CGPointMake(105, 640)];
[path addLineToPoint:CGPointMake(105, 340)];
//剑梁加固
[path moveToPoint:CGPointMake(95, 150)];
[path addLineToPoint:CGPointMake(95, 300)];
[path addLineToPoint:CGPointMake(105, 300)];
[path addLineToPoint:CGPointMake(105, 150)];
[path addLineToPoint:CGPointMake(94, 150)];
//小圆加固
[path moveToPoint:CGPointMake(100, 300)];
[path addLineToPoint:CGPointMake(100, 340)];
[path moveToPoint:CGPointMake(80, 320)];
[path addLineToPoint:CGPointMake(120, 320)];
//剑尾
[path moveToPoint:CGPointMake(100, -55)];
[path addLineToPoint:CGPointMake(60, -55)];
[path addLineToPoint:CGPointMake(100, -50)];
[path addLineToPoint:CGPointMake(140, -55)];
[path addLineToPoint:CGPointMake(100, -55)];
//剑柄
[path moveToPoint:CGPointMake(100, -50)];
[path addArcWithCenter:CGPointMake(100, -40) radius:10 startAngle:-M_PI_2 endAngle:-(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, -20) radius:10 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 0) radius:10 startAngle:-M_PI_2 endAngle:-(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 20) radius:10 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 40) radius:10 startAngle:-M_PI_2 endAngle:-(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 60) radius:10 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 80) radius:10 startAngle:-M_PI_2 endAngle:-(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 100) radius:10 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 120) radius:10 startAngle:-M_PI_2 endAngle:-(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 140) radius:10 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 140) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 120) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 100) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 80) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 60) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 40) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, 20) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:YES];
[path addArcWithCenter:CGPointMake(100, 0) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:NO];
[path addArcWithCenter:CGPointMake(100, -20) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:YES];
[path addArcWithCenter:CGPointMake(100, -40) radius:10 startAngle:M_PI_2 endAngle:(M_PI+M_PI_2) clockwise:NO];
#endif
#if 0
//正弦
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 100)];
[path addCurveToPoint:CGPointMake(200, 100) controlPoint1:CGPointMake(100, -100) controlPoint2:CGPointMake(100, 300)];
//
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(120, 0)];
[path addQuadCurveToPoint:CGPointMake(100, 100) controlPoint:CGPointMake(0, 50)];
[path addQuadCurveToPoint:CGPointMake(100, 200) controlPoint:CGPointMake(200, 150)];
[path addQuadCurveToPoint:CGPointMake(100, 300) controlPoint:CGPointMake(0, 250)];
[path addQuadCurveToPoint:CGPointMake(80, 400) controlPoint:CGPointMake(200, 350)];
[path moveToPoint:CGPointMake(100, -50)];
[path addLineToPoint:CGPointMake(100, 450)];
// [path addClip];
#endif
#if 0
path = [UIBezierPath bezierPath];
/**
1. lengths的值{10,10}表示先绘制10个点, 再跳过10个点, 如此反复
2. 如果把lengths值改为{10, 20, 10}, 则表示先绘制10个点, 跳过20个点, 绘制10个点, 跳过10个点, 再绘制20个点, 如此反复
注意count的值等于lengths数组的长度
3. phase参数表示在第一个虚线绘制的时候跳过多少个点
*/
//CGFloat pattern[] = {10, 10};
//[path setLineDash:pattern count:2 phase:0];
[path moveToPoint:CGPointMake(0, 100)]; //起点
[path addLineToPoint:CGPointMake(200, 100)]; //终点
// [path closePath];
#endif
NSMutableArray *pointArr = [NSMutableArray array];
for (int i=0; i<5; i++) {
CGPoint point = [self pointOfCircleWithCenter:CGPointMake(100, 100) angle:((M_PI*2)/5)*i radius:100];
[pointArr addObject:NSStringFromCGPoint(point)];
}
path = [UIBezierPath bezierPath];
for (int i=0; i<pointArr.count; i++) {
if (i == 0) {
[path moveToPoint:CGPointFromString(pointArr[i])];
}
else {
[path addLineToPoint:CGPointFromString(pointArr[i])];
}
}
[path closePath];
_shaperLayer = [CAShapeLayer layer];
_shaperLayer.frame = CGRectMake(100, 100, 200, 200);
// _shaperLayer.backgroundColor = [UIColor cyanColor].CGColor;
// _shaperLayer.masksToBounds = YES;
//要呈现的形状的路径
//如果路径扩展到层边界之外,只有当正常层屏蔽规则导致该条线时,它才会自动被剪辑到该层
//赋值时,路径被复制。默认为null
_shaperLayer.path = path.CGPath;
//虚线
NSArray *pattern1 = [NSArray arrayWithObjects:
[NSNumber numberWithInt:10],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:10],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:10],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:10],
[NSNumber numberWithInt:5],
nil];
[_shaperLayer setLineDashPattern:pattern1];
[_shaperLayer setLineDashPhase:0];
#if 0
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 100);
CGPathAddLineToPoint(path, NULL, 100, 100);
[shapeLayer setPath:path];
CGPathRelease(path);
#endif
/*
1. lineWidth为线的宽度,默认为1
2. miterLimit为最大斜接长度:
斜接长度指的是在两条线交汇处和外交之间的距离。
只有lineJoin属性为kCALineJoinMiter时miterLimit才有效。
边角的角度越小,斜接长度就会越大。
为了避免斜接长度过长,可以使用miterLimit属性。
如果斜接长度超过miterLimit的值,边角会以lineJoin的“bevel”即kCALineJoinBevel类型来显示。
*/
_shaperLayer.lineWidth = 5;
/*
1. lineCap为线端点类型,值有三个类型,分别:
kCALineCapButt 、kCALineCapRound 、kCALineCapSquare,默认值为Butt;
2. lineJoin为线连接类型,其值也有三个类型,分别:
kCALineJoinMiter、kCALineJoinRound、kCALineJoinBevel,默认值是Miter。
*/
_shaperLayer.lineJoin = kCALineJoinRound;
_shaperLayer.lineCap = kCALineJoinRound;
//设置描边色,默认无色
_shaperLayer.strokeColor = [UIColor blackColor].CGColor;
//填充路径的颜色,或不需要填充
//默认颜色为不透明的黑色
_shaperLayer.fillColor = [UIColor clearColor].CGColor;
//当在填充颜色的时候则就需要这种填充规则
//值有两种,非零和奇偶数,但默认是非零值。
_shaperLayer.fillRule = kCAFillRuleEvenOdd;
/*
这两个值被定义用于绘制边线轮廓路径的子区域。
该值必须在[0,1]范围,0代表路径的开始,1代表路径的结束。
在0和1之间的值沿路径长度进行线性插值。
strokeStart默认为0,strokeEnd默认为1。
*/
_shaperLayer.strokeStart = 0.0;
_shaperLayer.strokeEnd = 0.0;
[self.view.layer addSublayer:_shaperLayer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self startMoveShaper:0];
});
}
/**
圆点坐标: center
半径: radius
角度: angle
*/
- (CGPoint)pointOfCircleWithCenter:(CGPoint)center angle:(CGFloat)angle radius:(CGFloat)radius {
//(angle*M_PI)/180
CGFloat x = center.x + radius*cos(angle);
CGFloat y = center.y + radius*sin(angle);
return CGPointMake(x, y);
}
#pragma mark - animated
- (void)startMoveShaper:(CGFloat)delay {
CABasicAnimation *animated = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animated.toValue = @(1.0);
animated.duration = 5.0;
animated.beginTime = delay+CACurrentMediaTime();
animated.removedOnCompletion = NO;
animated.fillMode = kCAFillModeForwards;
animated.delegate = self;
[animated setValue:@"animatedEnd" forKey:@"name"];
[_shaperLayer addAnimation:animated forKey:@"start"];
// [self nextMoveShaper:5.0];
}
- (void)nextMoveShaper:(CGFloat)delay {
CABasicAnimation *animated = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
animated.toValue = @(1.0);
animated.duration = 5.0;
animated.beginTime = delay+CACurrentMediaTime();
animated.removedOnCompletion = NO;
animated.fillMode = kCAFillModeForwards;
animated.delegate = self;
[animated setValue:@"animatedStart" forKey:@"name"];
[_shaperLayer addAnimation:animated forKey:@"next"];
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
#if 1
NSString *name = [anim valueForKey:@"name"];
if ([name isEqualToString:@"animatedStart"]) {
[_shaperLayer removeAllAnimations];
[self startMoveShaper:0.0];
}
#endif
}
#pragma mark - timer
CGFloat _end = 0.0;
- (void)stopDisplayLink {
if (_displayLink) {
[_displayLink invalidate];
[_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
_displayLink = nil;
_end = 0.0;
}
}
- (void)startDisplayLink {
[self stopDisplayLink];
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction)];
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)displayLinkAction {
_end += 0.01;
_shaperLayer.strokeEnd = _end;
if (_end >= 1.0) {
[self stopDisplayLink];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end