drawRect:(CGRect)rect方法

来源:互联网 发布:quartus mac系统 编辑:程序博客网 时间:2024/05/19 06:49
1,默认只会在view第一次显示的时候调用(只能由系统自动调用,不能手动调用)

2,重绘(这个方法内部会重新调用drawRect:方法进行绘制)
     
     [self  setNeedsDisplay];  // 最好在set方法中写这个方法

     [self setNeedsDisplayInRect:(CGRect)rect];  // 精确位置的重绘   

3,画图形

        // 注:当系统调用drawRect:方法时,这个上下文,系统已经自动创建好了,用这条语句可以拿到这个系统已经创建的上下文。

CGContextRef tex = UIGraphicsGetCurrentContext();


(1)//画图    

//设置状态    

CGContextSetLineWidth(tex,10);//线宽    

CGContextSetLineCap(tex,kCGLineCapRound);//头部样式    

CGContextSetRGBStrokeColor(tex,1,0,0,1);//颜色    

CGContextSetRGBFillColor(tex,0,1,0,1);

        

(2)//画直线

        > 1,第一种    <br/>

CGContextMoveToPoint(tex,30,30);    

CGContextAddLineToPoint(tex,100,100);    

CGContextAddLineToPoint(tex,120,20);    

CGContextClosePath(tex);    

CGContextSetLineJoin(tex,kCGLineJoinBevel);

注:实际上 这种方式系统内部实现也是创建了一个path路径对象,

    > 2,第二种
CGContextRef ctx =UIGraphicsGetCurrentContext();

// 1,先创建一个路径    

CGPathRef path =CGPathCreateMutable();        

// 2,拼接路径 const CGAffineTransform 这个参数一般传NULL     

CGPathMoveToPoint(path, <#const CGAffineTransform *m#>, <#CGFloat x#>, <#CGFloat y#>);        

// 3,添加路径到上下文    

CGContextAddPath(ctx, path); 

// 4,渲染
CGContextFillPath(tex);

// 5,内存管理   

CGPathRelease(path);

// 或者用这个core foundation框架里的方法

CFRelease(path);

注:先创建一个路径对象,再把我们的做的东西缓存到这个路径,再把这个路径添加到上下文的缓存,上下文再把路径渲染到view中,画其它图形原理都是这样的。

       这其中需要内存管理,如果有Create/copy/retain,都要相应在添加到上下文后进行release


    (3)//画矩形
    > 1,第一种
     CGContextAddRect(tex,CGRectMake(20,20,160,100));
     CGContextFillPath(tex);
    > 2,第二种 (等价于第一种)
    CGContextFillRect(tex,CGRectMake(2020160100));
    > 3,第三种,用UIKit的OC方法
    UIRectFill(CGRectMake(120150160100));

    注释,画空心矩形时用stroke


    (4)//画椭圆    

    CGContextAddEllipseInRect(tex,CGRectMake(20,20,160,100));


    (5)//画弧
    CGContextAddArc(tex,100,100,40,0,M_PI,0);

    (6)//画文字

    NSString *str =@"slfjsdgjas";    

    [str drawAtPoint:CGPointMake(10,170)withAttributes:nil];


    (7)// 画图片

    UIImage *image = [UIImageimageNamed:@"图片名字"];    

    [image drawAtPoint:CGPointMake(0,0)];

        

    (8)// 平铺图形    [imagedrawAsPatternInRect:CGRectMake(0,0,200,200)];


     (9)//矩阵操作

    CGContextScaleCTM(tex,0.5,0.5);    

    CGContextRotateCTM(tex,M_PI_2);

    CGContextTranslateCTM(tex,0,100);

    (10)//裁减图片

    //图片画在后面    

    // 1,先画一个裁减的模具,然后再画要被裁减的图片    

    CGContextAddEllipseInRect(tex,CGRectMake(100,100,100,100));    

    // 2,将当前上下文的形状裁减出来,以后画东西只能画到裁减的图形里面,超出的部分都不显示

         CGContextClip(tex);    

UIImage *image1 = [UIImageimageNamed:@"图片名字"];    

[image1drawAtPoint:CGPointMake(100,100)];

     
    // 3,渲染
    CGContextFillPath(tex);

4,在UIKit中,为了简化绘图的实现,提供了一个"贝塞尔"路径的对象,方便绘图
      
   (1),画直线
     
    // 1,创建一个贝塞尔曲线路径
    UIBezierPath *path = [UIBezierPathbezierPath];

    // 2,移动到起始点    

[pathmoveToPoint:CGPointMake(30,30)];

    // 3,添加一条线,起始点,目标点    [

pathaddLineToPoint:CGPointMake(140,140)];    

    // 3.1,再加一条线     

[pathaddLineToPoint:CGPointMake(250,30)];

    // 4,闭合路径,让路径从起始点和"最后一条线"的目标点连线    

[pathclosePath];


    // 5,画线或填充

    // 画线    

[pathstroke];    

    //填充    

[pathfill];


     (2),画图像

    > 没有用到贝塞尔曲线

     // 1,拿到图像
     UIImage *image = [UIImageimageNamed:@"001.jpg"];

     // 2,画图像

          // 2.1,会根据指定的区域做拉伸处理      

 [image drawInRect:rect];     

     // 2.2,在指定点绘制原图,没有做拉伸效果,point是左上角的点      

[imagedrawAtPoint:CGPointMake(80,80)];

     // 2.3,Pattern -平铺     

[imagedrawAsPatternInRect:rect];

          > 用贝塞尔曲线
     (待补)

   (3),画文字

     1,设置文字属性

    //经常会使用字典来设置文本的属性,头文件在UIKit框架的第一个

    // 1.1,设置字体及大小
    UIFont *font = [UIFont systemFontOfSize:50]; // 设置字体大小

    // Bradley Hand 表示字体类型
    UIFont *font = [UIFontfontWithName:@"Bradley Hand"size:50]; 
    
    // familyNames 数组类型,存放着全部系统中已安装字体的
    NSLog(@"%@", [UIFontfamilyNames]); 
    

    // 1.2,设置字体阴影    

//阴影    

NSShadow *shadow = [[NSShadowalloc]init];    

//阴影颜色    

shadow.shadowColor = [UIColorgreenColor];    

//阴影偏移    

shadow.shadowOffset =CGSizeMake(-10, -10);    

//模糊度    

shadow.shadowBlurRadius =20.0;        

// 1.3,所有的属性都存放在字典中    

NSDictionary *dict =@{NSFontAttributeName: font,                          NSForegroundColorAttributeName: [UIColorredColor],                          NSStrokeColorAttributeName: [UIColoryellowColor],                          NSStrokeWidthAttributeName:@(5),                          NSShadowAttributeName: shadow};


     2,字符串 方法画上去

    // 2.1,在指定点绘字符串    

[strdrawAtPoint:CGPointMake(0,0)withAttributes:dict];

    // 2.2,在指定区域绘字符串 ,drawInRect会自动换行    

[strdrawInRect:rectwithAttributes:dict];

    // 2.3,NSStringDrawingUsesLineFragmentOrigin选项支持换行    

[strdrawWithRect:rectoptions:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil];


     //与 2.3 方法比较
          //计算文本的bounds
    CGRect r = [strboundingRectWithSize:rect.sizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil];

   (3),画矩形
     //绘制矩形 
    UIRectFill(CGRectMake(120,150,160,100));
     
     > 用贝塞尔曲线 
    // 创建 
    UIBezierPath *path = [UIBezierPathbezierPathWithRect:CGRectMake(x, y, w, h)];
    // 填充 

    [path fill];


      (4),裁减图形
     //如果画完图,再设置裁剪区域,无效
     UIRectClip(CGRectMake(20,20,160,100));
     
     > 用贝塞尔曲线
     CGRect r =CGRectMake(-40.0, -40.0,80.0,80.0);

     UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:r];     

//添加剪切区域     

[pathaddClip];


   (5),画圆弧
     > 用贝塞尔曲线 
     // 创建 
     UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:cradius:rstartAngle:sAendAngle:eAclockwise:YES];
     // 填充
    [path fill];

   (6),画椭圆

     > 用贝塞尔曲线
    UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(-30.0, -40.0,60.0,80.0)];
    [path fill];
0 0
原创粉丝点击