UIBezierPath+CAShapeLayer简单使用

如有发现问题, 请指正!

@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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容