IOS学习 绘图 UIBezierPath 绘基本图形、样式设置、渲染填充

来源:互联网 发布:网络摄像头哪个牌子好 编辑:程序博客网 时间:2024/05/22 01:38

#import "CZView.h"


@implementation CZView


-(void)drawRect:(CGRect)rect{

    //1.为什么要重写drawRect:方法

    // 只有在drawRect:才能获取上下文对象  --->才能绘图

    //2.rect参数:就是指当前视图的bounds属性

    

    //3.什么时候调用drawRect:方法

    

    // 1.视图第一次被显示的时候系统调用

    

    // 2.执行重绘的时候

    

    //4.不要手动调用这个方法 (系统会自动调用) drawRect:

    

    // 5.如果要手动调用执行重绘 [self setNeedsDisplay] / [self setNeedsDisplayInRect:<#(CGRect)#>]

    

    [selfdemo8];

}


- (void) demo9{

    // 1. 获取"图形上下文"

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    // 2.创建矩形路径

    UIBezierPath *path = [UIBezierPathbezierPathWithRect:CGRectMake(100,100, 200, 100)];

    // 3.创建一个圆心路径

    UIBezierPath *path1 = [UIBezierPathbezierPathWithArcCenter:CGPointMake(200,150) radius:80startAngle:0endAngle:M_PI *2 clockwise:1];

    // 4.创建一个矩形路径

    UIBezierPath *path2 = [UIBezierPathbezierPathWithRect:CGRectMake(250,30, 20, 200)];

    

    // 5.把刚刚创建的路径依次添加到上下文对象中

    CGContextAddPath(ctx, path2.CGPath);

    CGContextAddPath(ctx, path1.CGPath);

    CGContextAddPath(ctx, path.CGPath);

    

    // 说明:被覆盖过奇数次的点填充,被覆盖过偶数次的点不填充

    // 6.渲染

    CGContextDrawPath(ctx,kCGPathEOFill);

}


-(void)demo8{

    // 1. 获取"图形上下文"

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    //2.以顺时针的方式画一个大圆

    UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:CGPointMake(150,150) radius:100startAngle:0endAngle:M_PI *2 clockwise:YES];

    // 3.以逆时针画一个小圆

    UIBezierPath *path1 = [UIBezierPathbezierPathWithArcCenter:CGPointMake(150,150) radius:50startAngle:0endAngle:M_PI *2 clockwise:NO];

    //4.添加路径到上下文中

    CGContextAddPath(ctx, path1.CGPath);

    CGContextAddPath(ctx, path.CGPath);

    

    // 默认填充模式: nonzero winding number rule(非零绕数规则)从左到右跨过, +1。从右到左跨过, -1。最后如果为0,那么不填充,否则填充

    //渲染

    CGContextDrawPath(ctx,kCGPathFill);

}


//绘制弧线

-(void)demo7{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径对象

    //1.圆心

    CGPoint centerP =CGPointMake(150,150);

    

    //2.半径

    CGFloat radius =100;

    

    //3.起始角度

    CGFloat start =0;

    

    //4.结束角度

    CGFloat end =0.5*M_PI;//2Pi为整圆

    

    //5.clockwise 顺时针:YES  逆时针:NO

    

    UIBezierPath * path = [UIBezierPathbezierPathWithArcCenter:centerPradius:radius startAngle:startendAngle:end clockwise:YES];

    

    //回到圆心,画扇形

    [path addLineToPoint:centerP];

    [path closePath]; //关闭路径


    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

    CGContextStrokePath(ctx);

}



//绘制椭圆形

-(void)demo6{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径对象

    UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(50,50, 200, 100)];

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

//    CGContextStrokePath(ctx);

    CGContextFillPath(ctx);

}



//绘制圆形

-(void)demo5{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径对象

    UIBezierPath *path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(50,50, 200, 200)cornerRadius:100];

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

    CGContextStrokePath(ctx);

}


//绘制带圆角的矩形

-(void)demo4{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径对象

    UIBezierPath *path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(50,50, 200, 100)cornerRadius:30];

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

    CGContextFillPath(ctx);

}


//绘制正方形(简单写法)

-(void)demo3{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径添加到上下文中

    CGContextAddRect(ctx,CGRectMake(50,50, 100, 100));

    

    //设置绘图样式

    //1 设置线的stroke颜色

        [[UIColorredColor] setStroke];

    //设置填充颜色

    //    [[UIColor greenColor] setFill];

    //设置颜色

//    [[UIColor blueColor] set];

    //2.1.2 设置线宽

    CGContextSetLineWidth(ctx,10);

    

    //2.1.3 设置线头样式

    CGContextSetLineCap(ctx,kCGLineCapRound);

    

    //2.1.4 设置线的连接处样式

//    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    

    //渲染

    CGContextStrokePath(ctx);

//    CGContextFillPath(ctx);

}


//绘制四边形

-(void)demo2{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径对象

    UIBezierPath *path = [UIBezierPathbezierPathWithRect:CGRectMake(50,50, 200, 100)];

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);//path转化成CGPath

    

    //炫染

    //1.边线样式

//    CGContextDrawPath(ctx, kCGPathStroke);

//    CGContextStrokePath(ctx);

    

    //2.填充样式

//    CGContextDrawPath(ctx, kCGPathFill);

    CGContextFillPath(ctx);

}


//绘制线段

-(void)demo1{

    //获取上下文对象

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //创建路径,添加到上下文对象中

    //1.确定起点

    CGContextMoveToPoint(ctx,50, 50);

    

    //2.添加一条线

    CGContextAddLineToPoint(ctx,200, 200);

    

    //3.再添加一条线

    CGContextAddLineToPoint(ctx,50, 200);

    

    //4.闭合的线

    CGContextAddLineToPoint(ctx,50, 50);

//    CGContextClosePath(ctx);

    

    //另画一条线

    CGContextMoveToPoint(ctx,100, 50);

    CGContextAddLineToPoint(ctx,250, 50);


    //炫染

    CGContextStrokePath(ctx);

}

@end

0 0
原创粉丝点击