美文网首页
UIBezierPath 一些实用

UIBezierPath 一些实用

作者: Altitude2015 | 来源:发表于2018-02-28 17:10 被阅读0次

```

// 需要画线的视图

UIView*lineView = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame)- 100, 200, 200)];

lineView.backgroundColor = [UIColororangeColor];

[self.view addSubview:lineView];

// 线的路径

UIBezierPath*linePath = [UIBezierPathbezierPath];

// 起点

[linePath moveToPoint:CGPointMake(20, 20)];

// 其他点

[linePath addLineToPoint:CGPointMake(160, 160)];

[linePath addLineToPoint:CGPointMake(180, 50)];

多边形

[polygonPath closePath];// 添加一个结尾点和起点相同

CAShapeLayer*lineLayer = [CAShapeLayerlayer];

lineLayer.lineWidth = 2;

lineLayer.strokeColor = [UIColorgreenColor].CGColor;

lineLayer.path = linePath.CGPath;

lineLayer.fillColor =nil;// 默认为blackColor

[lineView.layer addSublayer:lineLayer];

2  划 椭圆

// 需要圆视图

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 130,CGRectGetMidY(self.view.frame) - 100, 260, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:view.bounds];

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.path = path.CGPath;

pathLayer.fillColor =nil;// 默认为blackColor

[view.layer addSublayer:pathLayer];

3 划圆

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame) - 100, 200, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:view.bounds];

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.path = path.CGPath;

// [view.layer addSublayer:pathLayer];

// pathLayer.fillColor = nil; // 默认为blackColor

view.layer.mask = pathLayer;// layer 的 mask属性,添加蒙版

4.画圆角矩形

// 需要画圆角矩形

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame) - 100, 200, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

UIBezierPath*path = [UIBezierPathbezierPathWithRoundedRect:view.bounds cornerRadius:50];

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.path = path.CGPath;

// pathLayer.fillColor = [UIColor lightGrayColor].CGColor; // 默认为blackColor

// [polygonView.layer addSublayer:polygonLayer];

view.layer.mask = pathLayer;// layer 的 mask属性,添加蒙版

5.画单角的圆角矩形的UIBezierPath相关方法

// 需要圆视图

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame) - 100, 200, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

UIBezierPath*path = [UIBezierPathbezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeftcornerRadii:CGSizeMake(100, 0)];

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.fillColor =nil;// 默认为blackColor

pathLayer.path = path.CGPath;

[view.layer addSublayer:pathLayer];

6.圆弧

画圆弧的UIBezierPath相关方法

+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

center:圆弧的中心,相对所在视图; radius:圆弧半径; startAngle:起始点的角度(相对角度坐标系0); endAngle:结束点的角度(相对角度坐标系0); clockwise:是否为顺时针方向。

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

在原有的线上添加一条弧线 。center:圆弧的中心,相对所在视图; radius:圆弧半径; startAngle:起始点的角度(相对角度坐标系0); endAngle:结束点的角度(相对角度坐标系0); clockwise:是否为顺时针方向。

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame) - 100, 200, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

CGPointviewCenter =CGPointMake(view.frame.size.width / 2.0, view.frame.size.height / 2.0);// 画弧的中心点,相对于view

UIBezierPath*path = [UIBezierPathbezierPathWithArcCenter:viewCenter radius:50.0 startAngle:0 endAngle:M_PI_2 clockwise:YES];

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.fillColor =nil;// 默认为blackColor

pathLayer.path = path.CGPath;

[view.layer addSublayer:pathLayer];

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame) - 100, 200, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

CGPointviewCenter =CGPointMake(view.frame.size.width / 2.0, view.frame.size.height / 2.0);// 画弧的中心点,相对于view

UIBezierPath*path = [UIBezierPathbezierPath];

[path moveToPoint:CGPointMake(50, 50)];

[path addLineToPoint:CGPointMake(100, 100)];

[path addArcWithCenter:viewCenter radius:50 startAngle:0 endAngle:M_PI clockwise:YES];// 添加一条弧线

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.fillColor =nil;// 默认为blackColor

pathLayer.path = path.CGPath;

[view.layer addSublayer:pathLayer];

折线和弧线构成的曲线

UIView* view = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100,CGRectGetMidY(self.view.frame) - 100, 200, 200)];

view.backgroundColor = [UIColororangeColor];

[self.view addSubview:view];

// 线的路径

CGPointviewCenter =CGPointMake(view.frame.size.width / 2.0, view.frame.size.height / 2.0);// 画弧的中心点,相对于view

UIBezierPath*path = [UIBezierPathbezierPath];

[path moveToPoint:CGPointMake(50, 50)];

[path addLineToPoint:CGPointMake(100, 100)];

[path addArcWithCenter:viewCenter radius:50 startAngle:0 endAngle:M_PI clockwise:YES];// 添加一条弧线

CAShapeLayer*pathLayer = [CAShapeLayerlayer];

pathLayer.lineWidth = 2;

pathLayer.strokeColor = [UIColorgreenColor].CGColor;

pathLayer.fillColor =nil;// 默认为blackColor

pathLayer.path = path.CGPath;

[view.layer addSublayer:pathLayer];

```

http://www.cnblogs.com/jaesun/p/iOS-CAShapeLayerUIBezierPath-hua-xian.html

相关文章

网友评论

      本文标题:UIBezierPath 一些实用

      本文链接:https://www.haomeiwen.com/subject/xavyittx.html