Quartz2D基本图形绘制

//当时图显示的时候调用默认调用一次

- (void)drawRect:(CGRect)rect {}

饼状图

-(void)drawPieChart{

    /**

     *  

     NSArray *array = @[@25,@25, @50];

     

     CGContextRef contextRef = UIGraphicsGetCurrentContext();

     CGPoint centerP = CGPointMake(125, 125);

     CGFloat radius = 120;

     CGFloat startA = 0;

     CGFloat angle = 0;

     CGFloat endA = 0;

     

     for (NSNumber *num in array) {

     

     startA = endA;

     angle = num.intValue / 100.0  * M_PI *2;

     endA = startA + angle;

     

     UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:startA endAngle:endA clockwise:YES];

     [path addLineToPoint:centerP];

     //    添加上下文

     CGContextAddPath(contextRef, path.CGPath);

     [[UIColor randomColor] set];

     

     //    渲染 不渲染的话后面的设置比如颜色会被覆盖掉

     CGContextFillPath(contextRef);

     }


     */

    

}


-(void)drawCircle{


    CGContextRef contextRef =UIGraphicsGetCurrentContext();

    CGFloat radius =60;

    CGPoint centerP =CGPointMake(50,50);

    UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:centerPradius:radius startAngle:0endAngle:M_PIclockwise:NO];

    

    CGContextAddPath(contextRef, path.CGPath);

//    CGContextStrokePath(contextRef);

    CGContextFillPath(contextRef);

}


-(void)dreaOval{

    CGContextRef contextRef =UIGraphicsGetCurrentContext();

    

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

    

    CGContextAddPath(contextRef, path.CGPath);

    CGContextStrokePath(contextRef);

    

}


-(void)draeRect{


    CGContextRef contextRef =UIGraphicsGetCurrentContext();

    //    绘制正方形

    //    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)];

    //    设圆角置正方形的

    UIBezierPath *path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(10,10,100,100)cornerRadius:20];

    

    CGContextAddPath(contextRef, path.CGPath);

    

    CGContextStrokePath(contextRef);

}


-(void)drawTrigon{


    CGContextRef contextRef =UIGraphicsGetCurrentContext();

    UIBezierPath *path = [UIBezierPathbezierPath];

    

    CGPoint startP =CGPointMake(10,10);

    [path moveToPoint:startP];

    [path addLineToPoint:CGPointMake(60,60)];

    [path addLineToPoint:CGPointMake(120,10)];

    //    从路径的终点连接到起点

    //    [path addLineToPoint:startP];

    //    关闭路径默认从路径的终点连接到起点

    [path closePath];

    CGContextAddPath(contextRef, path.CGPath);

    [[UIColorpurpleColor]setFill];

    [[UIColorblackColor]setStroke];

    CGContextSetLineWidth(contextRef,10);

    //    不填充

    //    CGContextStrokePath(contextRef);

    //    将划线的闭合空间填充颜色

    //    CGContextFillPath(contextRef);

    //    填充与线的颜色都显示模式 kCGPathFillStroke

    CGContextDrawPath(contextRef,kCGPathFillStroke);

    

}

-(void)drawQu{


    CGContextRef contextRet =UIGraphicsGetCurrentContext();

    

    UIBezierPath *path = [UIBezierPathbezierPath];

    

    CGPoint startPoint = CGPointMake(10,100);

    CGPoint endPoint =CGPointMake(100,100);

    CGPoint topPoint =CGPointMake(50,10);

    

    [path moveToPoint:startPoint];

    [path addQuadCurveToPoint:endPointcontrolPoint:topPoint];

    

    CGContextAddPath(contextRet, path.CGPath);

    CGContextStrokePath(contextRet);


}


-(void)drawLine2{


    //    上下文

    CGContextRef contextRef =UIGraphicsGetCurrentContext();

    //    拼接路径

    UIBezierPath *path = [UIBezierPathbezierPath];

    //    设置起点

    [path moveToPoint:CGPointMake(10,10)];

    [path addLineToPoint:CGPointMake(30,30)];

    

    //    重新新建一条路径显示

    //    [path moveToPoint:CGPointMake(20, 20)];

    //    [path addLineToPoint:CGPointMake(40, 50)];

    

    //    路径添加到上下文

    CGContextAddPath(contextRef, path.CGPath);

    

    //    设置线段宽度

    CGContextSetLineWidth(contextRef,10);

    //    设置线段圆角

    CGContextSetLineCap(contextRef,kCGLineCapRound);

    

    //    设置线段颜色

    //    CGContextSetRGBFillColor(contextRef, 1,0,0,1);

    [[UIColorgrayColor]set];

    

    

    //    渲染上下文

    CGContextStrokePath(contextRef);

}


-(void)drawLine{


    //获取上下文

    CGContextRef ref =UIGraphicsGetCurrentContext();

    //绘制路径

    UIBezierPath *path = [UIBezierPathbezierPath];

    

    //起始点

    [path moveToPoint:CGPointMake(10,10)];

    

    //    添加一条线到某点

    [path addLineToPoint:CGPointMake(200,200)];

    [path addLineToPoint:CGPointMake(300,10)];

    //    把路径添加到上下文 path.CGPath直接把UIKit的路径转化为CoreGraphics

    CGContextAddPath(ref, path.CGPath);

    //    把上下文渲染到视图

    CGContextStrokePath(ref);

}


绘制图片以及文字

-(void)drawImage{


//    UIImage *image = [UIImage imageNamed:@"123"];

//    [image drawAtPoint:CGPointZero];

//    [image drawAtPoint:rect];

//    //    平铺

//    [image drawAsPatternInRect:rect];

    //

}


-(void)drawText{


    NSString *text=@"1234zhangsdfjsld;fjs转身离开的减肥啦上;GDSF";

    CGRect  rectFrame =CGRectMake(0,0, 200,200);

    NSDictionary *dicty =@{

                            NSFontAttributeName :[UIFontsystemFontOfSize:20],

                            NSForegroundColorAttributeName:[UIColorredColor],

                            //                            NSStrokeWidthAttributeName:@5

                            };

    //    最自动换行

    [text drawInRect:rectFramewithAttributes:dicty];

    //    不会自动换行

    [text drawAtPoint:CGPointZerowithAttributes:dicty];

}


图形绘制的一些矩阵的操作

//    获取上下文

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    

//  平移上下文 注意:路径一定上下文操作矩阵之后

    CGContextTranslateCTM(contextRef, 50, 100);

//    旋转操作

    CGContextRotateCTM(contextRef, M_PI_4);

//    缩放操作

    CGContextScaleCTM(contextRef, 0.5, 0.3);

    

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-50, -100, 150, 200)];

    CGContextAddPath(contextRef, path.CGPath);

    CGContextFillPath(contextRef);




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章