CGGeometry.h ( 幾何類 )
一 幾個常用結(jié)構(gòu)體
struct CGPoint {
CGFloat x;
CGFloat y;
}; 定義一個點,設(shè)置x坐標和y坐標
struct CGSize {
CGFloat width;
CGFloat height;
}; 定義一個尺寸,設(shè)置寬度和高度
struct CGVector {
CGFloat dx;
CGFloat dy;
}; 定義一個二維矢量
struct CGRect {
CGPoint origin;
CGSize size;
}; 定義一個矩形
UIImage
todo
UIBezierPath:http://www.henishuo.com/uibezierpath-draw/
一.畫圖步驟
1.創(chuàng)建一個UIBezierPath對象
2.調(diào)用-moveToPoint:設(shè)置初始線段的起點
3.添加線或者曲線去定義一個或者多個子路徑
4.改變對象跟繪圖相關(guān)的屬性:(畫筆的顏色 樣式等等)
二.創(chuàng)建方法
****+ (****instancetype****)****bezierPath;****
最常用的創(chuàng)建方法,可以畫任意圖形
****+ (****instancetype****)****bezierPathWithRect****:****(****CGRect****)****rect;****
畫一個矩形曲線
****+ (****instancetype****)****bezierPathWithOvalInRect****:****(****CGRect****)****rect;****
通常用來畫圓和橢圓
****+ (****instancetype****)****bezierPathWithRoundedRect****:****(****CGRect****)rect****
**** cornerRadius**:****(****CGFloat****)****cornerRadius****;******
用來畫矩形,可以畫圓角矩形,第一個參數(shù)是矩形大小位置,第二個是圓角大小
****+ (****instancetype****)****bezierPathWithRoundedRect****:****(****CGRect****)rect****
**** byRoundingCorners:****(****UIRectCorner****)****corners******
**** cornerRadii:****(****CGSize****)****cornerRadii****;******
方法類似上面的,可以指定哪一個角是圓角
****+ (****instancetype****)****bezierPathWithArcCenter****:****(****CGPoint****)center****
**** radius:****(****CGFloat****)****radius******
**** startAngle:****(****CGFloat****)****startAngle******
**** endAngle:****(****CGFloat****)****endAngle******
**** clockwise:****(****BOOL****)****clockwise****;******
用來畫圓弧
****+ **(****instancetype****)****bezierPathWithCGPath****:****(****CGPathRef****)****CGPath****;******
三.效果(圖可以參見上面的網(wǎng)址)
1.畫三角形
UIBezierPath *path = [UIBezierPath bezierPath];
//設(shè)置路徑的起點
[path moveToPoint:CGPointMake(20, 20)];
//移動路徑到直線的另一點
[path addLineToPoint:CGPointMake(self.frame.size.width - 40, 20)];
[path addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20)];
// 最后的閉合線是可以通過調(diào)用closePath方法來自動生成的,也可以調(diào)用-addLineToPoint:方法來添加
// [path addLineToPoint:CGPointMake(20, 20)];
[path closePath];
// 設(shè)置線寬
path.lineWidth = 1.5;
// 設(shè)置填充顏色
UIColor *fillColor = [UIColor greenColor];
//啟動一下顏色
[fillColor set];
//給路線填充顏色
[path fill];
// 設(shè)置畫筆顏色
UIColor *strokeColor = [UIColor blueColor];
[strokeColor set];
// 根據(jù)我們設(shè)置的各個點連線
[path stroke];
2.畫矩形
1?普通矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRect:
CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40)];
2?圓角矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:
CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40)
cornerRadius:10];
3?特定角是圓角
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:
CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40)
byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
4?畫圓跟橢圓
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.width - 40)];
ps:傳入的矩形是一個正方形,那么就會得到一個圓,不是正方形是就是橢圓