iOS開發中Quartz2D的基本使用方式舉例_IOS

來源:互聯網
上載者:User

一、畫直線

代碼:

複製代碼 代碼如下:

//
//  YYlineview.m
//  03-畫直線
//
//  Created by apple on 14-6-9.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYlineview.h"

@implementation YYlineview


// 當自訂view第一次顯示出來的時候就會調用drawRect方法
- (void)drawRect:(CGRect)rect
{

    // 1.取得和當前視圖相關聯的圖形上下文(因為圖形上下文決定繪製的輸出目標)/
    // 如果是在drawRect方法中調用UIGraphicsGetCurrentContext方法擷取出來的就是Layer的上下文
    CGContextRef  ctx=UIGraphicsGetCurrentContext();//不需要*,同id
   
    // 2.繪圖(繪製直線), 儲存繪圖資訊
    // 設定起點
    CGContextMoveToPoint(ctx, 20, 100);
    //設定終點
    CGContextAddLineToPoint(ctx, 300, 100);
   
   
    //設定繪圖的狀態
    //設定線條的顏色為藍色
    CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);
    //設定線條的寬度
    CGContextSetLineWidth(ctx, 15);
    //設定線條起點和終點的樣式為圓角
    CGContextSetLineCap(ctx, kCGLineCapRound);
    //設定線條的轉角的樣式為圓角
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    //3.渲染(繪製出一條空心的線)
    CGContextStrokePath(ctx);
   
//    //注意線條不能渲染為實心的
//    CGContextFillPath(ctx);
   
   
   
    //設定第二條線
    //設定第二條線的起點
    CGContextMoveToPoint(ctx, 50, 200);
    //設定第二天線的終點(自動把上一條直線的終點當做起點)
    CGContextAddLineToPoint(ctx, 300, 60);
   
    //設定繪圖的狀態
//    CGContextSetRGBStrokeColor(ctx, 1.0, 0.7, 0.3, 1.0);
    //第二種設定顏色的方式
    [[UIColor grayColor] set];
    //設定線條的寬度
    CGContextSetLineWidth(ctx, 10);
    //設定線條的起點和終點的樣式
    CGContextSetLineCap(ctx, kCGLineCapButt);
   
    //渲染第二條線的圖形到view上
    //繪製一條空心的線
    CGContextStrokePath(ctx);
}


@end


效果:

二、畫三角形

代碼:

複製代碼 代碼如下:

//
//  YYrectview.m
//  02-畫三角形
//
//  Created by 孔醫己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYrectview.h"

@implementation YYrectview


- (void)drawRect:(CGRect)rect
{
    //1.獲得圖形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
   
    //2.繪製三角形
    //設定起點
    CGContextMoveToPoint(ctx, 20, 100);
    //設定第二個點
    CGContextAddLineToPoint(ctx, 40, 300);
    //設定第三個點
    CGContextAddLineToPoint(ctx, 200, 200);
    //設定終點
//     CGContextAddLineToPoint(ctx, 20, 100);
    //關閉起點和終點
    CGContextClosePath(ctx);
   
    // 3.渲染圖形到layer上
    CGContextStrokePath(ctx);
   
}


@end


效果:

提示:關閉起點和終點  CGContextClosePath(ctx);

三、畫四邊形

代碼:

複製代碼 代碼如下:

//
//  YYrect.m
//  03-畫四邊形
//
//  Created by 孔醫己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYrect.h"

@implementation YYrect


- (void)drawRect:(CGRect)rect
{

    //1.擷取圖形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.畫四邊形
    CGContextAddRect(ctx, CGRectMake(20, 20, 150, 100));
   
    // 如果要設定繪圖的狀態必須在渲染之前
    //    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
    // 繪製什麼類型的圖形(空心或者實心).就要通過什麼類型的方法設定狀態
    //    CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
   
    // 調用OC的方法設定繪圖的顏色
    //    [[UIColor purpleColor] setFill];
    //    [[UIColor blueColor] setStroke];
    // 調用OC的方法設定繪圖顏色(同時設定了實心和空心)
    //    [[UIColor greenColor] set];
    [[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] set];
   
   
    //3.渲染圖形到layer上
    //空心的
    CGContextStrokePath(ctx);
    //實心的
//    CGContextFillPath(ctx);
   
}


@end


提示:如果要設定繪圖的狀態必須在渲染之前。

效果(實心和空心):

四、畫圓

代碼1:

複製代碼 代碼如下:

- (void)drawRect:(CGRect)rect
{

    // 1.擷取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 畫圓
    CGContextAddArc(ctx, 100, 100, 50, 0, 2 * M_PI, 0);

    // 3.渲染 (注意, 畫線只能通過空心來畫)
//    CGContextFillPath(ctx);
    CGContextStrokePath(ctx);
   
}


效果:

代碼2:

複製代碼 代碼如下:

// 畫圓
    // 1.擷取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.畫圓
    CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 50));
   
    [[UIColor greenColor] set];
   
    // 3.渲染
    //    CGContextStrokePath(ctx);
    CGContextFillPath(ctx);

效果:

代碼3:

複製代碼 代碼如下:

// 畫橢圓
    // 1.擷取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.畫圓
    CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 100, 230));
   
    [[UIColor purpleColor] set];
   
    // 3.渲染
    //    CGContextStrokePath(ctx);
    CGContextFillPath(ctx);

效果:

五、畫圓弧

代碼1:

複製代碼 代碼如下:

// 畫圓弧
    // 1.擷取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.畫圓弧
    // x/y 圓心
    // radius 半徑
    // startAngle 開始的弧度
    // endAngle 結束的弧度
    // clockwise 畫圓弧的方向 (0 順時針, 1 逆時針)
    //    CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI_2, 0);
    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
    CGContextClosePath(ctx);
   
    // 3.渲染
    //     CGContextStrokePath(ctx);
    CGContextFillPath(ctx);

效果:

代碼2:

複製代碼 代碼如下:

// 1.擷取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.畫餅狀圖
    // 畫線
    CGContextMoveToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 100, 150);
    // 畫圓弧
    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
    //    CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);
   
    // 關閉路徑
    CGContextClosePath(ctx);
    [[UIColor brownColor]set];
   
   
    // 3.渲染 (注意, 畫線只能通過空心來畫)
    CGContextFillPath(ctx);
    //    CGContextStrokePath(ctx);

效果:

六、畫文字
代碼:

複製代碼 代碼如下:

//
//  YYtextview.m
//  04-寫文字
//
//  Created by 孔醫己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYtextview.h"

@implementation YYtextview


- (void)drawRect:(CGRect)rect
{
   
    // 畫文字
    NSString *str = @"的額搜風搜分手了粉色發俄雙方說法offFF瓦房你F回複F入會費WFH;飛;FN返回WFH;哦發貨;F回複;FHISFHSIFH我皮膚好APIFRHi分紅AWFHIOF威鋒網i";
   
    // 1.擷取上下文
    //    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.繪圖
    // 不推薦使用C語言的方法繪製文字, 因為quraz2d中的座標系和UIkit中的座標系不一致, 繪製出來的文字是顛倒的, 而且通過C語言的方法繪製文字相當麻煩
    //    CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)
    //    CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)
   
    // 繪製矩形
    // 1.擷取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.繪圖
    CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));
    // 3.渲染
    CGContextStrokePath(ctx);
   
   
//    NSMutableDictionary *md = [NSMutableDictionary dictionary];
//    // 設定文字顏色
//    md[NSForegroundColorAttributeName] =[UIColor redColor];
//    // 設定文字背景顏色
//    md[NSBackgroundColorAttributeName] = [UIColor greenColor];
//    // 設定文字大小
//    md[NSFontAttributeName] = [UIFont systemFontOfSize:20];
   
    //    將文字繪製到指點的位置
    //    [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
   
    //    將文字繪製到指定的範圍內, 如果一行裝不下會自動換行, 當文字超出範圍後就不顯示
    [str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];
}


@end


效果:

圖片

代碼1:

複製代碼 代碼如下:

//
//  YYimage.m
//  04-寫文字
//
//  Created by 孔醫己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{
 
    //    1.載入圖片到記憶體中
    UIImage *image = [UIImage imageNamed:@"me"];
   
   
    // 利用drawAsPatternInRec方法繪製圖片到layer, 是通過平鋪原有圖片
    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}


@end


效果(平鋪):

代碼2:

複製代碼 代碼如下:

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{
 
    //    1.載入圖片到記憶體中
    UIImage *image = [UIImage imageNamed:@"me"];
   
   
    // 利用OC方法將圖片繪製到layer上
 
    // 利用drawInRect方法繪製圖片到layer, 是通過展開原有圖片
        [image drawInRect:CGRectMake(0, 0, 200, 200)];
   
    // 利用drawAsPatternInRec方法繪製圖片到layer, 是通過平鋪原有圖片
//    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}


@end


效果(展開圖片):

代碼3:

複製代碼 代碼如下:

//
//  YYimage.m
//  04-寫文字
//
//  Created by 孔醫己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{
 
    //    1.載入圖片到記憶體中
    UIImage *image = [UIImage imageNamed:@"me"];
   
   
    // 利用OC方法將圖片繪製到layer上
   
    // 將圖片繪製到指定的位置
    [image drawAtPoint:CGPointMake(100, 100)];
    }


效果(把圖片繪製到一個固定的位置):

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.