Quartz2D学习笔记

 

 

1、drawRect方法

 

 1 //1.证明drawRect方法是在viewDidLoad后自动调用的,方便处理View的相关属性
 2 //    YQView * view = [[YQView alloc] initWithFrame:self.view.bounds];
 3 //
 4 //    [self.view addSubview:view];
 5 
 6     //2.init证明如果在初始化的时候没有设置rect的大小,将导致drawRect不能被自动调用
 7 //    YQView * view = [[YQView alloc] init];
 8 //
 9 //    [self.view addSubview:view];
10 
11     //3.手动调用drawRect
12     self.customView = [[YQView alloc] initWithFrame:self.view.bounds];
13 
14     [self.view addSubview:self.customView];
15 
16     self.customView.iconImage = [UIImage imageNamed:@"1.jpg"];
17 
18 //调用这个方法会出错,不能手动调用,系统不允许直接调用
19 //    [self.customView drawRect:self.view.bounds];
20 
21     //当需要手动调用drawRect方法的时候,实际上就是通知父类重新绘图
22     //setNeedsDisplay会异步自动调用drawRect方法
23     [self.customView setNeedsDisplay];

 

2、绘制线段、三角形、矩形、(椭)圆形、扇形

//1.画一条线段
- (void)drawLine:(CGContextRef)contextRef
{
    //先给一个起点
    CGContextMoveToPoint(contextRef, 20, 100);
    //再给一个终点
    CGContextAddLineToPoint(contextRef, 200, 200);
    
    //设置线的状态
    //线宽
    CGContextSetLineWidth(contextRef, 10);
    
    //颜色
//    CGContextSetRGBStrokeColor(contextRef, 0.4, 0.44, 0.99, 1.0);
    
    CGContextSetStrokeColorWithColor(contextRef, [UIColor redColor].CGColor);
    
    //风格设置
//    CGContextSetLineCap(contextRef, kCGLineCapRound);
    
    CGFloat lengths[] = {20, 10, 30};
    
    /**
     *  绘制虚线
     *
     *  @param c#>       作用域 description#>
     *  @param phase#>   起点的左移量 description#>
     *  @param lengths#> 实线和虚线的长度 description#>
     *  @param count#>   实线和虚线的循环个数(count必须等于lengths数组的长度) description#>
     *
     *  @return nil
     */
    CGContextSetLineDash(contextRef, 50, lengths, 3);
    
    //将图形绘制到view上面来(渲染)
    CGContextStrokePath(contextRef);
    
}
//2.画三角形
- (void)drawTriangle:(CGContextRef)contextRef
{
    CGContextMoveToPoint(contextRef, 30, 50);
    
    CGContextAddLineToPoint(contextRef, 50, 200);
    
    //如果连续添加多条线,它会把上一条线的终点作为下一条线的起点(即折线)
    CGContextAddLineToPoint(contextRef, 150, 80);
    
    //连接起点和终点(封起来)
    CGContextClosePath(contextRef);
    
//    CGContextAddLineToPoint(contextRef, 0, 0);
    
    //线宽
    CGContextSetLineWidth(contextRef, 10);
    
    //线的风格(拐角的风格)
    CGContextSetLineJoin(contextRef, kCGLineJoinRound);
    
    //
    CGContextSetStrokeColorWithColor(contextRef, [UIColor redColor].CGColor);
    
    //绘制边框内容
//    CGContextStrokePath(contextRef);

    //设置填充色
    CGContextSetFillColorWithColor(contextRef, [UIColor grayColor].CGColor);
    
    //绘制实心内容
    CGContextFillPath(contextRef);
    
}
//3.画矩形
- (void)drawRectangle:(CGContextRef)contextRef
{
    CGContextAddRect(contextRef, CGRectMake(100, 100, 100, 100));
    
    CGContextSetStrokeColorWithColor(contextRef, [UIColor yellowColor].CGColor);
    
    CGContextSetLineWidth(contextRef, 10);
    
    //空心的(画线轨迹)
//    CGContextStrokePath(contextRef);
    
    CGContextSetFillColorWithColor(contextRef, [UIColor greenColor].CGColor);

    //实心的
//    CGContextFillPath(contextRef);
    
    //同时显示线框和填充
    CGContextDrawPath(contextRef, kCGPathFillStroke);
    
    //以上三种渲染方式,只能使用一种,如果都写,只执行最先写的那个
    
}
//4.画圆(椭圆)
- (void)drawCircle:(CGContextRef)contextRef
{
    CGContextAddEllipseInRect(contextRef, CGRectMake(100, 100, 200, 100));
    
    CGContextFillPath(contextRef);
    
}
//5.扇形
- (void)drawArc:(CGContextRef)contextRef
{
//    CGContextMoveToPoint(contextRef, 100, 100);
    
    /**
     *  画扇形
     *
     *  @param contextRef   作用域
     *  @param x#>          原点x值(圆心) description#>
     *  @param y#>          原点y值(圆心) description#>
     *  @param radius#>     半径 description#>
     *  @param startAngle#> 开始的角度 description#>
     *  @param endAngle#>   结束的角度 description#>
     *  @param clockwise#>  方向(默认0顺时针) description#>
     *
     *  @return nil
     */
//    CGContextAddArc(contextRef, 100, 100, 50, 0, M_PI_2, 0);
//
//    CGContextAddLineToPoint(contextRef, 100, 100);
//    
//    CGContextStrokePath(contextRef);

    CGContextFillPath(contextRef);
//    
    //1.第一部分
    CGContextMoveToPoint(contextRef, 150, 150);
    
    CGContextAddArc(contextRef, 150, 150, 100, 0, 270 * M_PI / 180, 1);
    
    CGContextSetFillColorWithColor(contextRef, [UIColor cyanColor].CGColor);
    
    CGContextFillPath(contextRef);
    
    //2.第二部分
    CGContextMoveToPoint(contextRef, 150, 150);
    
    CGContextAddArc(contextRef, 150, 150, 100, 0, 120 * M_PI / 180, 0);
    
    CGContextSetFillColorWithColor(contextRef, [UIColor magentaColor].CGColor);
    
    CGContextFillPath(contextRef);
    
    CGContextMoveToPoint(contextRef, 150, 150);
    
    CGContextAddArc(contextRef, 150, 150, 100, 120 * M_PI / 180, 270 * M_PI / 180, 0);
    
    CGContextSetFillColorWithColor(contextRef, [UIColor yellowColor].CGColor);
    
    CGContextFillPath(contextRef);
}

posted on 2016-01-19 13:55  sea_star3  阅读(396)  评论(0编辑  收藏  举报

导航