文章搬運來源:blog.csdn.net/Calvin_zhou…面試
做者:PGzxcmarkdown
對iOS開發感興趣,能夠看一下做者的iOS交流羣:812157648,你們能夠在裏面吹水、交流相關方面的知識,羣裏還有我整理的有關於面試的一些資料,歡迎你們加羣,你們一塊兒開車框架
Quart 2D能作不少強大的事情,例如:oop
爲了便於搭建美觀的UI界面,iOS提供了UIKit框架,裏面有各類各樣的UI控件spa
利用UIKit框架提供的控件,拼拼湊湊,能搭建和實現一些簡單、常見的UI界面.net
可是,有些UI界面及其複雜、並且比較個性化,用普通的UI控件沒法實現,這時能夠利用Quart2D技術將控件內部的結構畫出來,自定義控件的樣子code
其實,iOS中大部分控件的內容都是經過Quart2D畫出來的orm
所以,Quart2D在iOS開發中很重的一個價值是:自定義view(自定義UI控件)blog
如何利用Quart2D自定義UI控件?繼承
- (void)drawRect:(CGRect)rect {
// NSLog(@"%@",NSStringFromCGRect(rect));
//1.獲取上下文
//CGContextRef CG CoreGraphics Ref引用
//目前學的上下文都跟UIGraphics有關,之後想直接獲取上下文,直接敲一個UIGraphics
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.設置繪圖信息(拼接路徑)
UIBezierPath *path=[UIBezierPath bezierPath];
//設置起點
[path moveToPoint:CGPointMake(10, 10)];
//添加一條線到某個點
[path addLineToPoint:CGPointMake(125, 125)];
[path addLineToPoint:CGPointMake(240, 10)];
//3.將路徑添加到上下文,直接把UIKit的路徑轉換成CoreGraphics,CG開頭就能轉
CGContextAddPath(ctx, path.CGPath);
//4.把上下文渲染到視圖,stroke描邊
CGContextStrokePath(ctx);
}
複製代碼
- (void)drawRect:(CGRect)rect {
//1.獲取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//設置起點
UIBezierPath *path=[UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10,125)];
//添加一條線到某個點
[path addLineToPoint:CGPointMake(230, 125)];
//設置起點
//[path moveToPoint:CGPointMake(10,10)];
//添加一條線到某個點
//[path addLineToPoint:CGPointMake(125,100)];
UIBezierPath *path1=[UIBezierPath bezierPath];
[path1 moveToPoint:CGPointMake(10, 10)];
[path1 addLineToPoint:CGPointMake(125, 100)];
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
CGContextAddPath(ctx,path1.CGPath);
//設置繪圖狀態
//設置線寬
CGContextSetLineWidth(ctx, 10);
CGContextSetLineCap(ctx, kCGLineCapRound);
[[UIColor redColor]set];
//4.渲染上下文到視圖
CGContextStrokePath(ctx);
}
複製代碼
- (void)drawRect:(CGRect)rect {
//1.獲取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2拼接路徑
UIBezierPath *path=[UIBezierPath bezierPath];
CGPoint startP=CGPointMake(10, 125);
CGPoint endP=CGPointMake(240, 125);
CGPoint controlP=CGPointMake(125,0);
[path moveToPoint:startP];
[path addQuadCurveToPoint:endP controlPoint:controlP];
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文到視圖
CGContextStrokePath(ctx);
}
複製代碼