日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios 贝塞尔曲线 颜色填充_iOS贝塞尔曲线(UIBezierPath)的基本使用方法

發布時間:2024/7/23 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios 贝塞尔曲线 颜色填充_iOS贝塞尔曲线(UIBezierPath)的基本使用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

UIBezierPath是對Core Graphics框架的一個封裝,使用UIBezierPath類我們可以畫出圓形(弧線)或者多邊形(比如:矩形)等形狀,所以在畫復雜圖形的時候會經常用到。

分析

首先我們先看一下,UIBezierPath有哪些重要的屬性:

1、 [color set]設置顏色,color為創建的UIColor對象

2、 [path stroke]填充view的線條的顏色,與[color set]配合使用 ,

3、 [path fill]填充整個view內部的顏色,與[color set]配合使用。

4、 path.lineWidth = 5.0; 這個很好理解了,就是劃線的寬度

5、 path.lineCapStyle 這個線段起點是終點的樣式,這個樣式有三種:

kCGLineCapButt

kCGLineCapRound

kCGLineCapSquare

6、 path.lineJoinStyle 這個屬性是用來設置兩條線連結點的樣式,同樣它也有三種樣式供我們選擇

kCGLineJoinMiter 直接連接

kCGLineJoinRound 圓滑銜接

kCGLineJoinBevel 斜角連接

使用

接下來,我們就看一下UIBezierPath到底應該怎么使用:

首先,我們先自定義一個UIView的子類,然后重寫- (void)drawRect:(CGRect)rect 方法,將創建圖形的方法寫到該方法中,下面是一些簡單的示例:

畫多邊形

UIBezierPath* aPath = [UIBezierPath bezierPath];

aPath.lineWidth = 15.0;

/*

kCGLineCapButt,

kCGLineCapRound,

kCGLineCapSquare

*/

aPath.lineCapStyle = kCGLineCapButt ; //終點(起點)樣式

/*

kCGLineJoinMiter,

kCGLineJoinRound,

kCGLineJoinBevel

*/

aPath.lineJoinStyle = kCGLineJoinBevel; //拐點樣式

[aPath moveToPoint:CGPointMake(150, 30)];//設置起始點

[aPath addLineToPoint:CGPointMake(250, 70)];//途經點

[aPath addLineToPoint:CGPointMake(210, 170)];//途經點

[aPath addLineToPoint:CGPointMake(90, 170)];//途經點

[aPath addLineToPoint:CGPointMake(50, 70)];//途經點

[aPath closePath];//通過調用closePath方法得到最后一條線

UIColor *strokeColor = [UIColor redColor];

[strokeColor set];

[aPath stroke];//設置線條顏色

UIColor *fillColor = [UIColor blueColor];

[fillColor set];

[aPath fill];//填充

多邊形.png

如果是創建四邊形可直接使用:

UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];

畫圓

UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)];

aPath.lineWidth = 5.0;

aPath.lineCapStyle = kCGLineCapRound; //線條拐角

aPath.lineJoinStyle = kCGLineCapRound; //終點處理

UIColor *color = [UIColor redColor];

[color set];

[aPath stroke];

弧形.png

如果要畫正圓,將rect的width和height設置為相等的值即可。

畫弧形

/*

ArcCenter: 原點

radius: 半徑

startAngle: 開始角度

endAngle: 結束角度

clockwise: 是否是順時針方向

*/

UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 300)

radius:80

startAngle:0

endAngle:pi

clockwise:NO];

aPath.lineWidth = 5.0;

aPath.lineCapStyle = kCGLineCapRound; //線條拐角

aPath.lineJoinStyle = kCGLineCapRound; //終點處理

UIColor *color = [UIColor redColor];

[color set]; //設置線條顏色

[aPath stroke];

圓形.png

畫二次曲線

UIBezierPath* aPath = [UIBezierPath bezierPath];

aPath.lineWidth = 5.0;

aPath.lineCapStyle = kCGLineCapRound; //線條拐角

aPath.lineJoinStyle = kCGLineCapRound; //終點處理

[aPath moveToPoint:CGPointMake(100, 100)];//設置初始點

//終點 controlPoint:切點(并不是拐彎處的高度,不懂的同學可以去看三角函數)

[aPath addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(150, 50)];

UIColor *color = [UIColor redColor];

[color set];

[aPath stroke];

二次曲線.png

畫三次曲線

UIBezierPath *path2 = [UIBezierPath bezierPath];

path2.lineWidth = 5.0;

path2.lineCapStyle = kCGLineCapRound;

path2.lineJoinStyle = kCGLineJoinRound;

[path2 moveToPoint:CGPointMake(0, 100)];

[path2 addCurveToPoint:CGPointMake(100, 100) controlPoint1:CGPointMake(25, 50) controlPoint2:CGPointMake(75, 150)];//兩個切點

UIColor *color = [UIColor redColor];

[color set];

[path2 stroke];

三次曲線.png

以上便是iOS中UIBezierPath最基本的使用方法了,在平時的開發中,我們經常將UIBezierPath與CALayer配合使用,下面是一個簡單的例子:

//創建CAShapeLayer對象

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.frame = CGRectMake(100, 100, 200, 200);//設置shapeLayer的尺寸和位置

shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充顏色為ClearColor

//設置線條的寬度和顏色

shapeLayer.lineWidth = 1.0f;

shapeLayer.strokeColor = [UIColor redColor].CGColor;

//創建一個圓形貝塞爾曲線

UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];

//將貝塞爾曲線設置為CAShapeLayer的path

shapeLayer.path = aPath.CGPath;

//將shapeLayer添加到視圖的layer上

[self.view.layer addSublayer:shapeLayer];

shapeLayer.png

總結:

到此為止,關于UIBezierPath最基本的使用就介紹完了,但是關于UIBezierPath在iOS中還有很多更加神奇的應用,有興趣的同學可以研究一下。

總結

以上是生活随笔為你收集整理的ios 贝塞尔曲线 颜色填充_iOS贝塞尔曲线(UIBezierPath)的基本使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。