如有发现问题, 请指正!
@interface ViewController ()
@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
/**
圆点坐标: 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