国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例

2020-07-26 03:20:59
字體:
供稿:網(wǎng)友

Graphics Context是圖形上下文,也可以理解為一塊畫布,我們可以在上面進(jìn)行繪畫操作,繪制完成后,將畫布放到我們的view中顯示即可,view看作是一個(gè)畫框.
CGContextRef功能強(qiáng)大,我們借助它可以畫各種圖形。開發(fā)過程中靈活運(yùn)用這些技巧,可以幫助我們提供代碼水平。

首先創(chuàng)建一個(gè)集成自UIView的,自定義CustomView類。
在CustomView.m中實(shí)現(xiàn)代碼。

復(fù)制代碼 代碼如下:

#import <QuartzCore/QuartzCore.h>

覆蓋DranRect方法,在此方法中繪制圖形。
CustomView寫好之后,需要使用到視圖控制器中。
使用方法:
復(fù)制代碼 代碼如下:

  CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:customView];

寫文字

復(fù)制代碼 代碼如下:

- (void)drawRect:(CGRect)rect
{
    //獲得當(dāng)前畫板
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //顏色
    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
    //畫線的寬度
    CGContextSetLineWidth(ctx, 0.25);
    //開始寫字
    [@"我是文字" drawInRect:CGRectMake(10, 10, 100, 30) withFont:font]; 
    [super drawRect:rect];
}

這段代碼就可以很漂亮的寫出四個(gè)大字:我是文字。很容易理解,每句話都有注釋。

畫直線

復(fù)制代碼 代碼如下:

- (void)drawRect:(CGRect)rect
{
    //獲得當(dāng)前畫板
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //顏色
    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
    //畫線的寬度
    CGContextSetLineWidth(ctx, 0.25);
    //頂部橫線
    CGContextMoveToPoint(ctx, 0, 10);
    CGContextAddLineToPoint(ctx, self.bounds.size.width, 10);
    CGContextStrokePath(ctx);
    [super drawRect:rect];
}

畫弧線

復(fù)制代碼 代碼如下:

CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);//改變畫筆顏色
    
    CGContextMoveToPoint(context, 140, 80);//開始坐標(biāo)p1
    
    //CGContextAddArcToPoint(CGContextRef c, CGFloat x1, CGFloat y1,CGFloat x2, CGFloat y2, CGFloat radius)
    //x1,y1跟p1形成一條線的坐標(biāo)p2,x2,y2結(jié)束坐標(biāo)跟p3形成一條線的p3,radius半徑,注意, 需要算好半徑的長度,
    CGContextAddArcToPoint(context, 148, 68, 156, 80, 10);
    
    CGContextStrokePath(context);//繪畫路徑
  

畫圓

復(fù)制代碼 代碼如下:

- (void)drawRect:(CGRect)rect
{
    //獲得當(dāng)前畫板
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //顏色
    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
    //畫線的寬度
    CGContextSetLineWidth(ctx, 0.25);
    //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
    // x,y為圓點(diǎn)坐標(biāo),radius半徑,startAngle為開始的弧度,endAngle為 結(jié)束的弧度,clockwise 0為順時(shí)針,1為逆時(shí)針。
    CGContextAddArc(ctx, 100, 20, 20, 0, 2*M_PI, 0); //添加一個(gè)圓
    CGContextDrawPath(ctx, kCGPathStroke); //繪制路徑
    [super drawRect:rect];
}

這個(gè)畫圓的公式你還記得嗎?你還知道M_PI是什么嗎?等于多少嗎?趕緊腦補(bǔ)一下吧!


畫大圓并填充顏色

復(fù)制代碼 代碼如下:

UIColor *aColor = [UIColor colorWithRed:1 green:0.0 blue:0 alpha:1];
    
    CGContextSetFillColorWithColor(context, aColor.CGColor);//填充顏色
    
    CGContextSetLineWidth(context, 3.0);//線的寬度
    
    CGContextAddArc(context, 250, 40, 40, 0, 2 * M_PI, 0); //添加一個(gè)圓
    //kCGPathFill填充非零繞數(shù)規(guī)則,kCGPathEOFill表示用奇偶規(guī)則,kCGPathStroke路徑,kCGPathFillStroke路徑填充,kCGPathEOFillStroke表示描線,不是填充
    
    CGContextDrawPath(context, kCGPathFillStroke); //繪制路徑加填充

畫矩形

復(fù)制代碼 代碼如下:

- (void)drawRect:(CGRect)rect
{
    //獲得當(dāng)前畫板
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //顏色
    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
    //畫線的寬度
    CGContextSetLineWidth(ctx, 0.25);
    CGContextAddRect(ctx, CGRectMake(2, 2, 30, 30));
    CGContextStrokePath(ctx);
    [super drawRect:rect];
}

畫扇形

復(fù)制代碼 代碼如下:

  //畫扇形,也就畫圓,只不過是設(shè)置角度的大小,形成一個(gè)扇形
    aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
    CGContextSetFillColorWithColor(context, aColor.CGColor);//填充顏色
    //以10為半徑圍繞圓心畫指定角度扇形
    CGContextMoveToPoint(context, 160, 180);
    CGContextAddArc(context, 160, 180, 30,  -60 * PI / 180, -120 * PI / 180, 1);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke); //繪制路徑
 

   
畫貝塞爾曲線
復(fù)制代碼 代碼如下:

 //二次曲線
    CGContextMoveToPoint(context, 120, 300);//設(shè)置Path的起點(diǎn)
    CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);//設(shè)置貝塞爾曲線的控制點(diǎn)坐標(biāo)和終點(diǎn)坐標(biāo)
    CGContextStrokePath(context);
    //三次曲線函數(shù)
    CGContextMoveToPoint(context, 200, 300);//設(shè)置Path的起點(diǎn)
    CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);//設(shè)置貝塞爾曲線的控制點(diǎn)坐標(biāo)和控制點(diǎn)坐標(biāo)終點(diǎn)坐標(biāo)
    CGContextStrokePath(context);

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 北流市| 商河县| 南宁市| 尤溪县| 连城县| 普宁市| 东明县| 余江县| 迭部县| 百色市| 阿拉善右旗| 山阴县| 特克斯县| 通辽市| 田东县| 民丰县| 武义县| 阿克苏市| 乐清市| 边坝县| 嵩明县| 阜平县| 大姚县| 玉环县| 沂水县| 道孚县| 惠东县| 琼海市| 宜州市| 安义县| 敖汉旗| 麻栗坡县| 兴文县| 东安县| 肇东市| 都匀市| 韶关市| 化州市| 连州市| 开平市| 建阳市|