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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发 贝塞尔曲线UIBezierPath(后记)

發(fā)布時(shí)間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发 贝塞尔曲线UIBezierPath(后记) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用CAShapeLayer與UIBezierPath可以實(shí)現(xiàn)不在view的drawRect方法中就畫(huà)出一些想要的圖形 。

1:UIBezierPath: UIBezierPath是在 UIKit 中的一個(gè)類(lèi),繼承于NSObject,可以創(chuàng)建基于矢量的路徑.此類(lèi)是Core Graphics框架關(guān)于path的一個(gè)OC封裝。使用此類(lèi)可以定義常見(jiàn)的圓形、多邊形等形狀 。我們使用直線(xiàn)、弧(arc)來(lái)創(chuàng)建復(fù)雜的曲線(xiàn)形狀。每一個(gè)直線(xiàn)段或者曲線(xiàn)段的結(jié)束的地方是下一個(gè)的開(kāi)始的地方。每一個(gè)連接的直線(xiàn)或者曲線(xiàn)段的集合成為subpath。一個(gè)UIBezierPath對(duì)象定義一個(gè)完整的路徑包括一個(gè)或者多個(gè)subpaths。

2:CAShapeLayer: CAShapeLayer顧名思義,繼承于CALayer。 每個(gè)CAShapeLayer對(duì)象都代表著將要被渲染到屏幕上的一個(gè)任意的形狀(shape)。具體的形狀由其path(類(lèi)型為CGPathRef)屬性指定。 普通的CALayer是矩形,所以需要frame屬性。CAShapeLayer初始化時(shí)也需要指定frame值,但 它本身沒(méi)有形狀,它的形狀來(lái)源于其屬性path 。CAShapeLayer有不同于CALayer的屬性,它從CALayer繼承而來(lái)的屬性在繪制時(shí)是不起作用的。

實(shí)例1:畫(huà)一個(gè)圓形

- (void)viewDidLoad {[super viewDidLoad];//創(chuàng)建出CAShapeLayerself.shapeLayer = [CAShapeLayer layer];self.shapeLayer.frame = CGRectMake(0, 0, 200, 200);//設(shè)置shapeLayer的尺寸和位置self.shapeLayer.position = self.view.center;self.shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充顏色為ClearColor//設(shè)置線(xiàn)條的寬度和顏色self.shapeLayer.lineWidth = 1.0f;self.shapeLayer.strokeColor = [UIColor redColor].CGColor;//創(chuàng)建出圓形貝塞爾曲線(xiàn)UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];//讓貝塞爾曲線(xiàn)與CAShapeLayer產(chǎn)生聯(lián)系self.shapeLayer.path = circlePath.CGPath;//添加并顯示[self.view.layer addSublayer:self.shapeLayer]; }

現(xiàn)在我們要用到CAShapeLayer的兩個(gè)參數(shù),strokeEnd和strokeStart

Stroke:用筆畫(huà)的意思

在這里就是起始筆和結(jié)束筆的位置

Stroke為1的話(huà)就是一整圈,0.5就是半圈,0.25就是1/4圈。以此類(lèi)推

如果我們把起點(diǎn)設(shè)為0,終點(diǎn)設(shè)為0.75

//設(shè)置stroke起始點(diǎn)

self.shapeLayer.strokeStart = 0;

self.shapeLayer.strokeEnd = 0.75;

?

實(shí)例2:畫(huà)兩個(gè)圓,其中一個(gè)圓表示進(jìn)度

//畫(huà)兩個(gè)圓形 -(void)createBezierPath:(CGRect)mybound {//外圓_trackPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:(mybound.size.width - 0.7)/ 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];;_trackLayer = [CAShapeLayer new];[self.view.layer addSublayer:_trackLayer];_trackLayer.fillColor = nil;_trackLayer.strokeColor=[UIColor grayColor].CGColor;_trackLayer.path = _trackPath.CGPath;_trackLayer.lineWidth=5;_trackLayer.frame = mybound;//內(nèi)圓_progressPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:(mybound.size.width - 0.7)/ 2 startAngle:- M_PI_2 endAngle:(M_PI * 2) * 0.7 - M_PI_2 clockwise:YES];_progressLayer = [CAShapeLayer new];[self.view.layer addSublayer:_progressLayer];_progressLayer.fillColor = nil;_progressLayer.strokeColor=[UIColor redColor].CGColor;_progressLayer.lineCap = kCALineCapRound;_progressLayer.path = _progressPath.CGPath;_progressLayer.lineWidth=5;_progressLayer.frame = mybound; }

實(shí)例3:創(chuàng)建一個(gè)轉(zhuǎn)動(dòng)的圓

- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor=[UIColor whiteColor];[self circleBezierPath];//用定時(shí)器模擬數(shù)值輸入的情況_timer = [NSTimer scheduledTimerWithTimeInterval:0.1target:selfselector:@selector(circleAnimationTypeOne)userInfo:nilrepeats:YES]; }-(void)circleBezierPath {//創(chuàng)建出CAShapeLayerself.shapeLayer = [CAShapeLayer layer];self.shapeLayer.frame = CGRectMake(0, 0, 150, 150);self.shapeLayer.position = self.view.center;self.shapeLayer.fillColor = [UIColor clearColor].CGColor;//設(shè)置線(xiàn)條的寬度和顏色self.shapeLayer.lineWidth = 2.0f;self.shapeLayer.strokeColor = [UIColor redColor].CGColor;//設(shè)置stroke起始點(diǎn)self.shapeLayer.strokeStart = 0;self.shapeLayer.strokeEnd = 0;//創(chuàng)建出圓形貝塞爾曲線(xiàn)UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 150, 150)];//讓貝塞爾曲線(xiàn)與CAShapeLayer產(chǎn)生聯(lián)系self.shapeLayer.path = circlePath.CGPath;//添加并顯示[self.view.layer addSublayer:self.shapeLayer]; }- (void)circleAnimationTypeOne {if (self.shapeLayer.strokeEnd > 1 && self.shapeLayer.strokeStart < 1) {self.shapeLayer.strokeStart += 0.1;}else if(self.shapeLayer.strokeStart == 0){self.shapeLayer.strokeEnd += 0.1;}if (self.shapeLayer.strokeEnd == 0) {self.shapeLayer.strokeStart = 0;}if (self.shapeLayer.strokeStart == self.shapeLayer.strokeEnd) {self.shapeLayer.strokeEnd = 0;} }- (void)circleAnimationTypeTwo {CGFloat valueOne = arc4random() % 100 / 100.0f;CGFloat valueTwo = arc4random() % 100 / 100.0f;self.shapeLayer.strokeStart = valueOne < valueTwo ? valueOne : valueTwo;self.shapeLayer.strokeEnd = valueTwo > valueOne ? valueTwo : valueOne; }

實(shí)例4:通過(guò)點(diǎn)畫(huà)線(xiàn)組成一個(gè)五邊線(xiàn)

//畫(huà)一個(gè)五邊形 -(void)fiveAnimation {UIBezierPath *aPath = [UIBezierPath bezierPath];//開(kāi)始點(diǎn) 從上左下右的點(diǎn)[aPath moveToPoint:CGPointMake(100,100)];//劃線(xiàn)點(diǎn)[aPath addLineToPoint:CGPointMake(60, 140)];[aPath addLineToPoint:CGPointMake(60, 240)];[aPath addLineToPoint:CGPointMake(160, 240)];[aPath addLineToPoint:CGPointMake(160, 140)];[aPath closePath];//設(shè)置定點(diǎn)是個(gè)5*5的小圓形(自己加的)UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100-5/2.0, 0, 5, 5)];[aPath appendPath:path];CAShapeLayer *shapelayer = [CAShapeLayer layer];//設(shè)置邊框顏色shapelayer.strokeColor = [[UIColor redColor]CGColor];//設(shè)置填充顏色 如果只要邊 可以把里面設(shè)置成[UIColor ClearColor]shapelayer.fillColor = [[UIColor blueColor]CGColor];//就是這句話(huà)在關(guān)聯(lián)彼此(UIBezierPath和CAShapeLayer):shapelayer.path = aPath.CGPath;[self.view.layer addSublayer:shapelayer]; }

實(shí)例5:畫(huà)一條虛線(xiàn)

//畫(huà)一條虛線(xiàn) -(void)createDottedLine {CAShapeLayer *shapeLayer = [CAShapeLayer layer];[shapeLayer setBounds:self.view.bounds];[shapeLayer setPosition:self.view.center];[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];// 設(shè)置虛線(xiàn)顏色為blackColor[shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];// 3.0f設(shè)置虛線(xiàn)的寬度[shapeLayer setLineWidth:1.0f];[shapeLayer setLineJoin:kCALineJoinRound];// 3=線(xiàn)的寬度 1=每條線(xiàn)的間距[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:3],[NSNumber numberWithInt:1],nil]];// Setup the pathCGMutablePathRef path = CGPathCreateMutable();CGPathMoveToPoint(path, NULL, 0, 89);CGPathAddLineToPoint(path, NULL, 320,89);[shapeLayer setPath:path];CGPathRelease(path);// 可以把self改成任何你想要的UIView, 下圖演示就是放到UITableViewCell中的[[self.view layer] addSublayer:shapeLayer]; }

實(shí)例6:畫(huà)一個(gè)弧線(xiàn)

//畫(huà)一個(gè)弧線(xiàn) -(void)createCurvedLine {UIBezierPath* aPath = [UIBezierPath bezierPath];aPath.lineWidth = 5.0;aPath.lineCapStyle = kCGLineCapRound; //線(xiàn)條拐角aPath.lineJoinStyle = kCGLineCapRound; //終點(diǎn)處理[aPath moveToPoint:CGPointMake(20, 100)];[aPath addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 0)];self.CurvedLineLayer=[CAShapeLayer layer];self.CurvedLineLayer.path=aPath.CGPath;[self.view.layer addSublayer:self.CurvedLineLayer]; }

另外.....

我的愿望是.......

世界和平.........

轉(zhuǎn)載于:https://www.cnblogs.com/ioshe/p/5481841.html

總結(jié)

以上是生活随笔為你收集整理的iOS开发 贝塞尔曲线UIBezierPath(后记)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。