iOS開(kāi)發(fā)中Quartz2D的基本使用方式舉例
一、畫(huà)直線(xiàn)
代碼:
//
// YYlineview.m
// 03-畫(huà)直線(xiàn)
//
// Created by apple on 14-6-9.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYlineview.h"
@implementation YYlineview
// 當(dāng)自定義view第一次顯示出來(lái)的時(shí)候就會(huì)調(diào)用drawRect方法
- (void)drawRect:(CGRect)rect
{
// 1.取得和當(dāng)前視圖相關(guān)聯(lián)的圖形上下文(因?yàn)閳D形上下文決定繪制的輸出目標(biāo))/
// 如果是在drawRect方法中調(diào)用UIGraphicsGetCurrentContext方法獲取出來(lái)的就是Layer的上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();//不需要*,同id
// 2.繪圖(繪制直線(xiàn)), 保存繪圖信息
// 設(shè)置起點(diǎn)
CGContextMoveToPoint(ctx, 20, 100);
//設(shè)置終點(diǎn)
CGContextAddLineToPoint(ctx, 300, 100);
//設(shè)置繪圖的狀態(tài)
//設(shè)置線(xiàn)條的顏色為藍(lán)色
CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);
//設(shè)置線(xiàn)條的寬度
CGContextSetLineWidth(ctx, 15);
//設(shè)置線(xiàn)條起點(diǎn)和終點(diǎn)的樣式為圓角
CGContextSetLineCap(ctx, kCGLineCapRound);
//設(shè)置線(xiàn)條的轉(zhuǎn)角的樣式為圓角
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//3.渲染(繪制出一條空心的線(xiàn))
CGContextStrokePath(ctx);
// //注意線(xiàn)條不能渲染為實(shí)心的
// CGContextFillPath(ctx);
//設(shè)置第二條線(xiàn)
//設(shè)置第二條線(xiàn)的起點(diǎn)
CGContextMoveToPoint(ctx, 50, 200);
//設(shè)置第二天線(xiàn)的終點(diǎn)(自動(dòng)把上一條直線(xiàn)的終點(diǎn)當(dāng)做起點(diǎn))
CGContextAddLineToPoint(ctx, 300, 60);
//設(shè)置繪圖的狀態(tài)
// CGContextSetRGBStrokeColor(ctx, 1.0, 0.7, 0.3, 1.0);
//第二種設(shè)置顏色的方式
[[UIColor grayColor] set];
//設(shè)置線(xiàn)條的寬度
CGContextSetLineWidth(ctx, 10);
//設(shè)置線(xiàn)條的起點(diǎn)和終點(diǎn)的樣式
CGContextSetLineCap(ctx, kCGLineCapButt);
//渲染第二條線(xiàn)的圖形到view上
//繪制一條空心的線(xiàn)
CGContextStrokePath(ctx);
}
@end
效果:
二、畫(huà)三角形
代碼:
//
// YYrectview.m
// 02-畫(huà)三角形
//
// Created by 孔醫(yī)己 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.繪制三角形
//設(shè)置起點(diǎn)
CGContextMoveToPoint(ctx, 20, 100);
//設(shè)置第二個(gè)點(diǎn)
CGContextAddLineToPoint(ctx, 40, 300);
//設(shè)置第三個(gè)點(diǎn)
CGContextAddLineToPoint(ctx, 200, 200);
//設(shè)置終點(diǎn)
// CGContextAddLineToPoint(ctx, 20, 100);
//關(guān)閉起點(diǎn)和終點(diǎn)
CGContextClosePath(ctx);
// 3.渲染圖形到layer上
CGContextStrokePath(ctx);
}
@end
效果:
提示:關(guān)閉起點(diǎn)和終點(diǎn) CGContextClosePath(ctx);
三、畫(huà)四邊形
代碼:
//
// YYrect.m
// 03-畫(huà)四邊形
//
// Created by 孔醫(yī)己 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.畫(huà)四邊形
CGContextAddRect(ctx, CGRectMake(20, 20, 150, 100));
// 如果要設(shè)置繪圖的狀態(tài)必須在渲染之前
// CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
// 繪制什么類(lèi)型的圖形(空心或者實(shí)心).就要通過(guò)什么類(lèi)型的方法設(shè)置狀態(tài)
// CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
// 調(diào)用OC的方法設(shè)置繪圖的顏色
// [[UIColor purpleColor] setFill];
// [[UIColor blueColor] setStroke];
// 調(diào)用OC的方法設(shè)置繪圖顏色(同時(shí)設(shè)置了實(shí)心和空心)
// [[UIColor greenColor] set];
[[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] set];
//3.渲染圖形到layer上
//空心的
CGContextStrokePath(ctx);
//實(shí)心的
// CGContextFillPath(ctx);
}
@end
提示:如果要設(shè)置繪圖的狀態(tài)必須在渲染之前。
效果(實(shí)心和空心):
四、畫(huà)圓
代碼1:
- (void)drawRect:(CGRect)rect
{
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 畫(huà)圓
CGContextAddArc(ctx, 100, 100, 50, 0, 2 * M_PI, 0);
// 3.渲染 (注意, 畫(huà)線(xiàn)只能通過(guò)空心來(lái)畫(huà))
// CGContextFillPath(ctx);
CGContextStrokePath(ctx);
}
效果:
代碼2:
// 畫(huà)圓
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.畫(huà)圓
CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 50));
[[UIColor greenColor] set];
// 3.渲染
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
效果:
代碼3:
// 畫(huà)橢圓
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.畫(huà)圓
CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 100, 230));
[[UIColor purpleColor] set];
// 3.渲染
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
效果:
五、畫(huà)圓弧
代碼1:
// 畫(huà)圓弧
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.畫(huà)圓弧
// x/y 圓心
// radius 半徑
// startAngle 開(kāi)始的弧度
// endAngle 結(jié)束的弧度
// clockwise 畫(huà)圓弧的方向 (0 順時(shí)針, 1 逆時(shí)針)
// 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.畫(huà)餅狀圖
// 畫(huà)線(xiàn)
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 100, 150);
// 畫(huà)圓弧
CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
// CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);
// 關(guān)閉路徑
CGContextClosePath(ctx);
[[UIColor brownColor]set];
// 3.渲染 (注意, 畫(huà)線(xiàn)只能通過(guò)空心來(lái)畫(huà))
CGContextFillPath(ctx);
// CGContextStrokePath(ctx);
效果:
六、畫(huà)文字
代碼:
//
// YYtextview.m
// 04-寫(xiě)文字
//
// Created by 孔醫(yī)己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYtextview.h"
@implementation YYtextview
- (void)drawRect:(CGRect)rect
{
// 畫(huà)文字
NSString *str = @"的額搜風(fēng)搜分手了粉色發(fā)俄雙方說(shuō)法offFF瓦房你F回復(fù)F入會(huì)費(fèi)WFH;飛;FN返回WFH;哦發(fā)貨;F回復(fù);FHISFHSIFH我皮膚好APIFRHi分紅AWFHIOF威鋒網(wǎng)i";
// 1.獲取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.繪圖
// 不推薦使用C語(yǔ)言的方法繪制文字, 因?yàn)閝uraz2d中的坐標(biāo)系和UIkit中的坐標(biāo)系不一致, 繪制出來(lái)的文字是顛倒的, 而且通過(guò)C語(yǔ)言的方法繪制文字相當(dāng)麻煩
// 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];
// // 設(shè)置文字顏色
// md[NSForegroundColorAttributeName] =[UIColor redColor];
// // 設(shè)置文字背景顏色
// md[NSBackgroundColorAttributeName] = [UIColor greenColor];
// // 設(shè)置文字大小
// md[NSFontAttributeName] = [UIFont systemFontOfSize:20];
// 將文字繪制到指點(diǎn)的位置
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
// 將文字繪制到指定的范圍內(nèi), 如果一行裝不下會(huì)自動(dòng)換行, 當(dāng)文字超出范圍后就不顯示
[str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];
}
@end
效果:
圖片
代碼1:
//
// YYimage.m
// 04-寫(xiě)文字
//
// Created by 孔醫(yī)己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYimage.h"
@implementation YYimage
- (void)drawRect:(CGRect)rect
{
// 1.加載圖片到內(nèi)存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用drawAsPatternInRec方法繪制圖片到layer, 是通過(guò)平鋪原有圖片
[image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
@end
效果(平鋪):
代碼2:
#import "YYimage.h"
@implementation YYimage
- (void)drawRect:(CGRect)rect
{
// 1.加載圖片到內(nèi)存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用OC方法將圖片繪制到layer上
// 利用drawInRect方法繪制圖片到layer, 是通過(guò)拉伸原有圖片
[image drawInRect:CGRectMake(0, 0, 200, 200)];
// 利用drawAsPatternInRec方法繪制圖片到layer, 是通過(guò)平鋪原有圖片
// [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
@end
效果(拉伸圖片):
代碼3:
//
// YYimage.m
// 04-寫(xiě)文字
//
// Created by 孔醫(yī)己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYimage.h"
@implementation YYimage
- (void)drawRect:(CGRect)rect
{
// 1.加載圖片到內(nèi)存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用OC方法將圖片繪制到layer上
// 將圖片繪制到指定的位置
[image drawAtPoint:CGPointMake(100, 100)];
}
效果(把圖片繪制到一個(gè)固定的位置):
相關(guān)文章
IOS中Weex 加載 .xcassets 中的圖片資源的實(shí)例詳解
這篇文章主要介紹了IOS中Weex 加載 .xcassets 中的圖片資源的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文介紹能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08詳解IOS開(kāi)發(fā)之實(shí)現(xiàn)App消息推送(最新)
這篇文章主要介紹了詳解IOS開(kāi)發(fā)之實(shí)現(xiàn)App消息推送(最新),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12iOS?WKWebView秒開(kāi)方案實(shí)戰(zhàn)記錄
從iOS8開(kāi)始,就引入了新的瀏覽器控件WKWebView,用于取代UIWebView,下面這篇文章主要給大家介紹了關(guān)于iOS?WKWebView秒開(kāi)方案的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12簡(jiǎn)單好用的iOS導(dǎo)航欄封裝.runtime屬性控制實(shí)例代碼
這篇文章主要給大家介紹了簡(jiǎn)單好用的iOS導(dǎo)航欄封裝.runtime屬性控制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的方法
這篇文章主要給大家介紹了關(guān)于iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS使用音頻處理框架The Amazing Audio Engine實(shí)現(xiàn)音頻錄制播放
這篇文章主要為大家詳細(xì)介紹了iOS使用音頻處理框架The Amazing Audio Engine實(shí)現(xiàn)音頻錄制播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04