IOS開發之Quartz2D繪圖,iosquartz2d繪圖

來源:互聯網
上載者:User

IOS開發之Quartz2D繪圖,iosquartz2d繪圖


//自訂繪製圖形,調用drawRect方法

- (void)drawRect:(CGRect)rect {

   //首先拿到上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

//    [self drawLine:context];



}


#pragma mark -繪製線條

- (void)drawLine:(CGContextRef)context

{

    //1.擷取上下文

    //context

    

    //2.建立繪製的路徑(path)

    //路徑

    //CGPathRef不可變

    //CGMutablePathRef可變路徑

    CGMutablePathRef path = CGPathCreateMutable();

    

    //設定起始點

    CGPathMoveToPoint(path, NULL, 50, 50);

    

    //連線到下一點

    CGPathAddLineToPoint(path, NULL, 200, 200);

    CGPathAddLineToPoint(path, NULL, 50, 200);

    

    //關閉路徑

    CGPathCloseSubpath(path);

    

   //3.把路徑添加到上下文中

    CGContextAddPath(context, path);

    

    //4.設定內容相關的屬性

    //線條的顏色

    CGContextSetRGBStrokeColor(context, 41/255.0, 110/255.0, 222/255.0, 1);

    //填充的顏色

    CGContextSetRGBFillColor(context, 150/255.0, 50/255.0, 100/255.0, 1);

    

    //線條寬度

    CGContextSetLineWidth(context, 5);

    

    //連接點的樣式

    CGContextSetLineJoin(context, kCGLineJoinRound);

    

    //5.繪製路徑

    /*

     kCGPathFill,   //只填充

     kCGPathEOFill, //異或

     kCGPathStroke, //只畫線

     kCGPathFillStroke,     //填充和畫線

     kCGPathEOFillStroke

     */

    CGContextDrawPath(context, kCGPathFillStroke);

    

    //6.釋放路徑

    CGPathRelease(path);

    

}

#pragma mark -繪製矩形

- (void)drawShapeRect:(CGContextRef)context

{

    //方法一

    /*

    //設定繪製的地區

    CGRect rect = CGRectMake(40, 40, 100, 200);

    

    //設定地區添加到上下文

    CGContextAddRect(context, rect);

    

    //UIKit封裝的設定顏色的方法

    [[UIColor redColor] setStroke];

    [[UIColor blueColor] setFill];

    

    //繪製

    CGContextDrawPath(context, kCGPathFillStroke);

     */

    //方法二

    //UIKit

    CGRect rect = CGRectMake(40, 40, 100, 200);

    

    [[UIColor redColor] setStroke];

    [[UIColor blueColor] setFill];

    

   //調用,直接開始繪圖

    UIRectFill(rect);   //填充

    UIRectFrame(rect);  //架構

 

}


#pragma mark -繪製圓形

- (void)drawCircle:(CGContextRef)context

{

    //圓

    /*

     * x,y    圓心

     * radius 半徑

     * startAngle endAngle 起始和結束的角度(單位弧度)

     * clockwise 順時針 0 / 逆時針 1

     */

    //CGContextAddArc(context, 160, 200, 50, 0, M_PI_2, 1);

    

    //內切圓

    CGRect rect = CGRectMake(50, 50, 200, 100);

    [[UIColor orangeColor] setStroke];

    UIRectFrame(rect);

    

    CGContextAddEllipseInRect(context, CGRectMake(50, 50, 200, 100));

    

    [[UIColor redColor] setStroke];

    [[UIColor brownColor] setFill];

    

    CGContextDrawPath(context, kCGPathFillStroke);

}


#pragma mark -貝茲路徑

- (void)drawCurve:(CGContextRef)context

{

    //起始點

    CGContextMoveToPoint(context, 20, 50);

    

    /*

     * 1x, 1y第一條切線的終點

     * 2x, 2y第二條切線的起始點

     * x, y第二條切線的終點

     */

//    CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);

    

    CGContextAddQuadCurveToPoint(context, 160, 20, 300, 200);

    

    CGContextDrawPath(context, kCGPathStroke);


}


#pragma mark -繪製文本

- (void)drawText:(CGContextRef)context

{

    //1.繪製的文字

    NSString *str = @"wxhl34";

    

    //2.定義繪製的地區

    CGRect rect = CGRectMake(50, 50, 200, 50);

    

    UIRectFrame(rect);

    

    //3.設定繪製的樣式

   //文本段落的樣式

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

    style.lineBreakMode = NSLineBreakByCharWrapping;

    style.alignment = NSTextAlignmentRight;

    

    NSDictionary *attr = @{

                           NSFontAttributeName:[UIFont systemFontOfSize:20],

                           NSParagraphStyleAttributeName:style

                           };

    

    

    //4.繪製

    [str drawInRect:rect withAttributes:attr];

}


#pragma mark -繪製圖片

- (void)drawImage:(CGContextRef)context

{

    //圖片

    UIImage *image = [UIImage imageNamed:@"baymax.jpg"];

    

   //1.以指定點為起點繪製

    [image drawAtPoint:CGPointMake(20, 400)];

    

   //2.在指定地區繪製,會自動展開

    //[image drawInRect:CGRectMake(0, 0, 100, 200)];


    

    //座標系轉換

    //CG   UIKit

   //儲存當前的上下文

    CGContextSaveGState(context);

    

    //CGContextTranslateCTM(context, 0, 200);

    //y軸取反

    CGContextScaleCTM(context, 1, -1);

    //y軸上移200

    CGContextTranslateCTM(context, 0, -200);

    

    

    /*

    CGContextRotateCTM(context, M_PI);      //旋轉

    CGContextScaleCTM(context, -1, 1);      //縮放

    CGContextTranslateCTM(context, 0, -200);    //平移

     */

    

    //CG

    //UIImage --> CGImageRef

    //UIKit       Core Graphics     AppKit

    CGContextDrawImage(context, CGRectMake(0, 0, 320, 200), image.CGImage);

    

   //恢複之前儲存的上下文

    CGContextRestoreGState(context);

}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.