UIBezierPath用法

来源:互联网 发布:ubuntu更新 编辑:程序博客网 时间:2024/06/06 03:06


#import<Foundation/Foundation.h>

@protocolUIBezierPath__ <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:100startAngle:0endAngle:1.57clockwise: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.lineCapStyle
aPath.lineCapStyle = kCGLineCapRound;
//线条拐角 设置线条两端的样式为圆角

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




@end
0 0
原创粉丝点击