什么是 CGConTextRef
CGConTextRef相當(dāng)于一個(gè)畫(huà)布铜邮,我們可以在上面畫(huà)各種各樣的圖形,而這個(gè)畫(huà)布又是在 View 上寨蹋。所以我們對(duì) CGConTextRef 進(jìn)行的一切操作相當(dāng)于在 View 上進(jìn)行松蒜。
在 View 中系統(tǒng)回自動(dòng)執(zhí)行 - (void)drawRect:(CGRect)rect 這個(gè)方法,所以我們需要在這個(gè)方法中已旧,創(chuàng)建一個(gè)畫(huà)布秸苗,然后對(duì)其進(jìn)行操作。那么 CGConTextRef 到底可以做些什么呢运褪?我們先來(lái)看看代碼
第一:寫(xiě)字
//獲得當(dāng)前畫(huà)板
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
/**
* 字的大小
*
* @param c#> 畫(huà)板名 description#>
* @param red#> 紅 description#>
* @param green#> 綠 description#>
* @param blue#> 藍(lán) description#>
* @param alpha#> 透明度 description#>
*/ CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
//畫(huà)線(xiàn)的寬度 參數(shù)為 畫(huà)板 寬度
CGContextSetLineWidth(ctx, 0.25);
//開(kāi)始把NSString寫(xiě)到畫(huà)布上 參數(shù):X,Y,W,H
[@"我是大展惊楼!" drawInRect:CGRectMake(100, 100, 100, 30)
// 布局畫(huà)板
[super drawRect:rect]; // 這個(gè)方法只用加載一次
}
第二:畫(huà)線(xiàn)
//獲得當(dāng)前畫(huà)板
- (void)drawRect:(CGRect)rect
{
//獲得當(dāng)前畫(huà)板
CGContextRef ctx = UIGraphicsGetCurrentContext();
//顏色
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
//畫(huà)線(xiàn)的寬度
CGContextSetLineWidth(ctx, 0.25);
//頂部橫線(xiàn)
CGContextMoveToPoint(ctx, 0, 10);
CGContextAddLineToPoint(ctx, self.bounds.size.width, 10);
CGContextStrokePath(ctx);
[super drawRect:rect];
}
}
第三:畫(huà)圓
//獲得當(dāng)前畫(huà)板
- (void)drawRect:(CGRect)rect
{
//獲得當(dāng)前畫(huà)板
CGContextRef ctx = UIGraphicsGetCurrentContext();
//顏色
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
//畫(huà)線(xiàn)的寬度
CGContextSetLineWidth(ctx, 0.25);
//頂部橫線(xiàn)
CGContextMoveToPoint(ctx, 0, 10);
CGContextAddLineToPoint(ctx, self.bounds.size.width, 10);
CGContextStrokePath(ctx);
[super drawRect:rect];
}
}
第四:畫(huà)矩形
- (void)drawRect:(CGRect)rect
{
//獲得當(dāng)前畫(huà)板
CGContextRef ctx = UIGraphicsGetCurrentContext();
//顏色
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
//畫(huà)線(xiàn)的寬度
CGContextSetLineWidth(ctx, 0.25);
CGContextAddRect(ctx, CGRectMake(2, 2, 30, 30));
CGContextStrokePath(ctx);
[super drawRect:rect];
}
我們來(lái)綜合使用下
- (void)drawRect:(CGRect)rect
{
//獲得當(dāng)前畫(huà)板
CGContextRef ctx = UIGraphicsGetCurrentContext();
//顏色
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
//畫(huà)線(xiàn)的寬度
CGContextSetLineWidth(ctx, 0.25);
//開(kāi)始寫(xiě)字
[@"我是大展!" drawInRect:CGRectMake(100, 100, 100, 30) withFont:[UIFont systemFontOfSize:20]];
[@"我是大展吐句!" drawInRect:CGRectMake(200, 300, 100, 30) withFont:[UIFont systemFontOfSize:20]];
[@"我是大展胁后!" drawInRect:CGRectMake(100, 500, 100, 30) withFont:[UIFont systemFontOfSize:20]];
//畫(huà)線(xiàn)
CGContextMoveToPoint(ctx, 0, 300);
CGContextAddLineToPoint(ctx, self.bounds.size.width, 100);
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, 0, 300);
CGContextAddLineToPoint(ctx, self.bounds.size.width, 500);
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, self.bounds.size.width, 500);
CGContextAddLineToPoint(ctx, 0, 700);
CGContextStrokePath(ctx);
// 畫(huà)圓
// 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為開(kāi)始的弧度,endAngle為 結(jié)束的弧度屯断,clockwise 0為順時(shí)針文虏,1為逆時(shí)針。
CGContextAddArc(ctx, 300, 600, 20, 0, 2*M_PI, 0); //添加一個(gè)圓
CGContextDrawPath(ctx, kCGPathStroke); //繪制路徑
[super drawRect:rect];
}
屏幕快照 2016-01-22 19.29.43.png
謝謝殖演!
如有雷同氧秘,你就是抄我的!?????? --大展