UIBezierPath用法



#import <Foundation/Foundation.h>

@protocol UIBezierPath__ <NSObject>

// 根据一个矩形画曲线
+ (UIBezierPath *)bezierPathWithRect:(CGRect)rect

// 根据矩形框的内切圆画曲线
+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect

// 根据矩形画带圆角的曲线
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius

// 在矩形中,可以针对四角中的某个角加圆角
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
参数:
corners:
枚举值,可以选择某个角
cornerRadii:
圆角的大小

// 以某个中心点画弧线

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(
BOOL )clockwise;
参数:
center:
弧线中心点的坐标
radius:
弧线所在圆的半径
startAngle:
弧线开始的角度值
endAngle:
弧线结束的角度值
clockwise:
是否顺时针画弧线

// 画二元曲线,一般和 moveToPoint 配合使用
- (
void )addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
参数:
endPoint:
曲线的终点
controlPoint:
画曲线的基准点

// 以三个点画一段曲线,一般和 moveToPoint 配合使用
- (
void )addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
参数:
endPoint:
曲线的终点
controlPoint1:
画曲线的第一个基准点
controlPoint2:
画曲线的第二个基准点



// Path operations on the current graphics context

- (
void )fill;
- (
void )stroke;




 Only
override drawRect: if you perform custom drawing.
 An empty implementation adversely affects performance during animation.
- (
void )drawRect:(CGRect)rect
{
    UIColor *color = [UIColor redColor];
    [color set];
// 设置线条颜色

    UIBezierPath* aPath = [UIBezierPath bezierPath];
    aPath.lineWidth =
5.0 ;

    aPath.lineCapStyle = kCGLineCapRound;
// 线条拐角
    aPath.lineJoinStyle = kCGLineCapRound;
// 终点处理

   
// Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(
100.0 , 0.0 )];

   
// Draw the lines
    [aPath addLineToPoint:CGPointMake(
200.0 , 40.0 )];
    [aPath addLineToPoint:CGPointMake(
160 , 140 )];
    [aPath addLineToPoint:CGPointMake(
40.0 , 140 )];
    [aPath addLineToPoint:CGPointMake(
0.0 , 40.0 )];
    [aPath closePath];
// 第五条线通过调用 closePath 方法得到的

    [aPath stroke];
//Draws line 根据坐标点连线
}






// 开始图像绘图
UIGraphicsBeginImageContext(
self .view.bounds.size);

// 创建一个五边形
// 创建 UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPath];

// 线条拐角
path.lineCapStyle = kCGLineCapRound;  
// 设置线条两端的样式为圆角


// 终点处理
path.lineJoinStyle = kCGLineCapRound;
// path.lineJoinStyle = kCGLineCapButt;

// 设置线宽
path.lineWidth =
10 ;

// 设置线的颜色
[[UIColor blueColor] set];

// 使用方法 moveToPoint: 去设置初始线段的起点
[path moveToPoint:CGPointMake(
100 , 0 )];

// 添加线段 连续的创建 line ,每一个 line 的起点都是先前的终点,终点就是指定的点。
[path addLineToPoint:CGPointMake(
200 , 40 )];
[path addLineToPoint:CGPointMake(
160 , 140 )];
[path addLineToPoint:CGPointMake(
40 , 140 )];
[path addLineToPoint:CGPointMake(
0 , 40 )];
[path closePath];


// 执行绘画
[path stroke];
[path fill];



// 创建矩形
//+ (UIBezierPath *)bezierPathWithRect:(CGRect)rect
UIBezierPath *rect = [UIBezierPath bezierPathWithRect:CGRectMake(
220 , 30 , 120 , 100 )];


rect.lineCapStyle = kCGLineCapRound;
rect.lineJoinStyle = kCGLineCapRound;
rect.lineWidth =
10 ;
[[UIColor redColor] set];
[rect stroke];
//
//
//    // 在矩形中,可以针对四角中的某个角加圆角
//    //+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
参数:
corners: 枚举值,可以选择某个角
cornerRadii: 圆角的大小
//
//
//
//
//    // 创建圆形或者椭圆形
//    //+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
//    // 这个方法根据传入的 rect 矩形参数绘制一个内切曲线。当传入的 rect 是一个正方形时,绘制的图像是一个内切圆;当传入的 rect 是一个长方形时,绘制的图像是一个内切椭圆。
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(
40 , 170 , 100 , 100 )];


circle.lineCapStyle = kCGLineCapRound;
circle.lineJoinStyle = kCGLineCapRound;
circle.lineWidth =
10 ;
[[UIColor yellowColor] set];
[circle stroke];
//
//
//
//    // 创建一段弧
//    //+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
参数:
center: 弧线中心点的坐标
radius: 弧线所在圆的半径
startAngle: 弧线开始的角度值
endAngle: 弧线结束的角度值
clockwise: 是否顺时针画弧线
//
UIBezierPath *arc = [UIBezierPath bezierPathWithArcCenter:CGPointMake(
240 , 190 ) radius: 100 startAngle: 0 endAngle: 1.57 clockwise: YES ];

arc.lineCapStyle = kCGLineCapRound;
arc.lineJoinStyle = kCGLineCapRound;
arc.lineWidth =
10 ;
[[UIColor grayColor] set];
[arc stroke];
//
//
//
//
//  // 绘制二次贝塞尔曲线
//    //- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
参数:
endPoint: 曲线的终点
controlPoint: 画曲线的基准点
//
//
UIBezierPath *curve = [UIBezierPath bezierPath];
curve.lineCapStyle = kCGLineCapRound;
curve.lineJoinStyle = kCGLineCapRound;
curve.lineWidth =
10 ;
[[UIColor greenColor] set];


[curve moveToPoint:CGPointMake(
40 , 370 )];
[curve addQuadCurveToPoint:CGPointMake(
300 , 370 ) controlPoint:CGPointMake( 150 , 500 )];

[curve stroke];

//
//
//   // 绘制三次贝塞尔曲线
//   // - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
参数:
endPoint: 曲线的终点
controlPoint1: 画曲线的第一个基准点
controlPoint2: 画曲线的第二个基准点
//
//
UIBezierPath *curve1 = [UIBezierPath bezierPath];
curve1.lineCapStyle = kCGLineCapRound;
curve1.lineJoinStyle = kCGLineCapRound;
curve1.lineWidth =
10 ;
[[UIColor orangeColor] set];


[curve1 moveToPoint:CGPointMake(
40 , 550 )];
[curve1 addCurveToPoint:CGPointMake(
300 , 550 ) controlPoint1:CGPointMake( 100 , 450 ) controlPoint2:CGPointMake( 200 , 650 )];



[curve1 stroke];
//
//










//   Context 中获取图像,并显示在界面上
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imaView = [[UIImageView alloc] initWithImage:img];
[
self .view addSubview:imaView];
[imaView release];

// 保存绘制好的图片到文件中
// 先将图片转换为二进制数据,然后再将图片写到文件中
// 转换为 JPER 格式
// NSData *data=UIImageJPEGRepresentation(img, 1);
// 转化为 PNG 格式
NSData *data=UIImagePNGRepresentation(img);
[data writeToFile:
@"/Users/xalo/Desktop/abc.png" atomically: YES ];




1.l ineCapStyle
aPath.lineCapStyle = kCGLineCapRound;
// 线条拐角   设置线条两端的样式为圆角

2.l ineJoinStyle
aPath.lineJoinStyle = kCGLineCapRound; //终点处理




@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值