博客> UIBezierPath的使用
UIBezierPath的使用
2017-12-09 22:02 评论:0 阅读:75 进击的可乐SAMA

如果要使用UIBezierPath,则需要与CAShapeLayer这个类一起使用,下面说一下几个简单的用法


//实例化一个CAShapeLayer的类
CAShapeLayer * layer = [[CAShapeLayer alloc] init];

//设置填充颜色
layer.fillColor = [UIColor clearColor].CGColor;

//边线颜色
layer.strokeColor = [UIColor redColor].CGColor;

//边线宽度
layer.lineWidth = 1.f;

//创建各种类型的UIBezierPath
UIBezierPath * path;
if ([self.type isEqualToString:@"矩形"]) {//画出矩形

    path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];

}else if ([self.type isEqualToString:@"圆角"]){//画出带圆角的图形

    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:8.f];

}else if ([self.type isEqualToString:@"标准圆弧"]){//画出标准圆弧

    path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:50.f startAngle:M_PI endAngle:M_PI_2 clockwise:YES];
    /*
     第一个参数:圆弧原点
     第二个参数:圆弧半径
     第三个参数:圆弧起点
     第四个参数:圆弧终点
     第五个参数:是否顺时针
     */

}else if ([self.type isEqualToString:@"曲线1"]){//画出有一个转折点的曲线

    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(100, 200)];
    [path addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(200, 300)];

}else if ([self.type isEqualToString:@"曲线2"]){//画出有两个转折点的曲线

    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 200)];
    [path addCurveToPoint:CGPointMake(350, 200) controlPoint1:CGPointMake(150, 100) controlPoint2:CGPointMake(250, 300)];

}else if ([self.type isEqualToString:@"综合"]){//综合使用

    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 400)];
    [path addLineToPoint:CGPointMake(0, 300)];
    [path addQuadCurveToPoint:CGPointMake(375, 300) controlPoint:CGPointMake(375 / 2, 200)];
    [path addLineToPoint:CGPointMake(375, 400)];
    [path closePath];

    }

//设置layer的path属性
layer.path = path.CGPath;
[self.view.layer addSublayer:layer];
收藏
0
sina weixin mail 回到顶部