iOS开发 贝塞尔曲线UIBezierPath
生活随笔
收集整理的這篇文章主要介紹了
iOS开发 贝塞尔曲线UIBezierPath
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
UIBezierPath基礎
UIBezierPath對象是CGPathRef數據類型的封裝。每一個直線段或者曲線段的結束的地方是下一個的開始的地方。每一個連接的直線或者曲線段的集合成為subpath。一個UIBezierPath對象定義一個完整的路徑包括一個或者多個subpaths。
創建和使用一個path對象的過程是分開的。創建path是第一步,包含一下步驟:
(1)創建一個Bezier path對象。
(2)使用方法moveToPoint:去設置初始線段的起點。
(3)添加line或者curve去定義一個或者多個subpaths。
(4)改變UIBezierPath對象跟繪圖相關的屬性。
UIBezierPath 代碼示例:
- (void)drawRect:(CGRect)rect {#pragma mark - 1. 畫一個紅色五角形// 創建bezierPath對象UIBezierPath *aPath = [UIBezierPath bezierPath];// 設置顏色[[UIColor redColor] set];// 定義線條寬度aPath.lineWidth = 3;/** stroke時候線條終點的效果** kCGLineCapButt,* kCGLineCapRound,* kCGLineCapSquare*/aPath.lineCapStyle = kCGLineCapRound;/** stroke時候線條連接處的效果** kCGLineJoinMiter,* kCGLineJoinRound,* kCGLineJoinBevel*/aPath.lineJoinStyle = kCGLineJoinMiter;// 設置形狀的起點[aPath moveToPoint:CGPointMake(100, 10)];// 畫四條線[aPath addLineToPoint:CGPointMake(200.0, 50.0)];[aPath addLineToPoint:CGPointMake(160, 150)];[aPath addLineToPoint:CGPointMake(40.0, 150)];[aPath addLineToPoint:CGPointMake(10.0, 50.0)];// 第五條線通過 closePath 得到[aPath closePath];// 根據坐標將線畫出來[aPath stroke];[[UIColor yellowColor] set];[aPath fill];#pragma mark - 2. 創建矩形UIBezierPath *aPath2 = [UIBezierPath bezierPathWithRect:CGRectMake(10, 160, 100, 30)];aPath2.lineWidth = 2;aPath2.lineCapStyle = kCGLineCapRound;aPath2.lineJoinStyle = kCGLineJoinRound;[[UIColor blackColor] set];[aPath2 stroke];[aPath2 removeAllPoints];aPath2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(120, 160, 100, 30) cornerRadius:5];[aPath2 stroke];#pragma mark - 3. 創建圓形或者橢圓形/** 使用如下方法創建內切圓或者內切橢圓* + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect * 當傳入的rect是一個正方形時,繪制的圖像是一個內切圓;當傳入的rect是一個長方形時,繪制的圖像是一個內切橢圓。*/UIBezierPath *aPath3 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 200, 100, 100)];[aPath3 moveToPoint:CGPointMake(60, 250)];[aPath3 stroke];[aPath3 removeAllPoints];[aPath3 addArcWithCenter:CGPointMake(60, 250) radius:4 startAngle:0 endAngle:M_PI*2 clockwise:YES];[aPath3 fill];#pragma mark - 4. 使用UIBezierPath創建一段弧線// 參數 closewise: 順時針方向UIBezierPath *aPath4 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(60, 250) radius:70 startAngle:M_PI_4 endAngle:M_PI_4+M_PI_2 clockwise:YES];aPath4.lineWidth = 3;[aPath4 stroke];#pragma mark - 5. 繪制 二次/三次 貝塞爾曲線UIBezierPath *aPath5 = [UIBezierPath bezierPath];[aPath5 moveToPoint:CGPointMake(10, 330)];[aPath5 addQuadCurveToPoint:CGPointMake(130, 330) controlPoint:CGPointMake(10, 400)];// 三次貝塞爾曲線[[UIColor redColor] set];[aPath5 addCurveToPoint:CGPointMake(320, 330) controlPoint1:CGPointMake(140, 400) controlPoint2:CGPointMake(320, 100)];[aPath5 stroke];/// 讓控件在 path上面移動的效果. ****************************************CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];moveAnim.path = aPath5.CGPath;moveAnim.removedOnCompletion = YES;[self.imageView.layer addAnimation:moveAnim forKey:nil];#pragma mark - 6. 將文字和圖片畫在畫布上去.//將文字畫在畫布上[@"Walden" drawAtPoint:CGPointMake(10, 10) withAttributes:nil];UIImage *image = [UIImage imageNamed:@"image1.png"];[image drawInRect:CGRectMake(10, 400, 100, 100)];}轉載于:https://my.oschina.net/whforever/blog/700375
總結
以上是生活随笔為你收集整理的iOS开发 贝塞尔曲线UIBezierPath的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php7的redis和yaf的扩展安装
- 下一篇: android 官方DrawerLayo