UIBezierPath

UIBezierPath

  • 畫線
- (void)drawRect:(CGRect)rect {
    //1.創(chuàng)建貝塞爾路徑的實例
    UIBezierPath *path = [UIBezierPath bezierPath];
    //2.勾勒圖形
    [path moveToPoint:CGPointMake(40, 40)];
    [path addLineToPoint:CGPointMake(140, 40)];
    [path addLineToPoint:CGPointMake(140, 140)];
    [path addLineToPoint:CGPointMake(40, 140)];
//    [path addLineToPoint:CGPointMake(40, 40)];
    [path closePath];


    [path moveToPoint:CGPointMake(40, 200)];
    [path addLineToPoint:CGPointMake(140, 200)];
    [path addLineToPoint:CGPointMake(140, 300)];
    [path addLineToPoint:CGPointMake(40, 300)];
    [path closePath];

    //設置描邊線的寬度
    path.lineWidth = 10;
    //焦點的樣式
//    kCGLineJoinMiter, //尖的
//    kCGLineJoinRound, //圓的
//    kCGLineJoinBevel  //斜的  角被砍掉
    path.lineJoinStyle = kCGLineJoinBevel;
    //線兩端的樣式
//    kCGLineCapButt,  //方的
//    kCGLineCapRound, //圓的 多出一塊
//    kCGLineCapSquare //方的 多出一塊
    path.lineCapStyle = kCGLineCapSquare;

    //設置 描邊顏色
    [[UIColor redColor] setStroke];
    //設置 填充顏色
    [[UIColor greenColor] setFill];
    //描邊
    [path stroke];
    //填充
    [path fill];

}
  • 畫圓
- (void)drawRect:(CGRect)rect {
    UIBezierPath *path = [UIBezierPath bezierPath];
    //clockwise 為YES順時針  為NO逆時針
    [path addArcWithCenter:CGPointMake(100, 100) radius:80 startAngle:M_PI_2 * 3 endAngle:0 clockwise:YES];
    [path addLineToPoint:CGPointMake(100, 100)];
    [path closePath];

    //移動畫筆
//    [path moveToPoint:CGPointMake(100, 180)];
//    [path addArcWithCenter:CGPointMake(100, 100) radius:80 startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
//    [path addLineToPoint:CGPointMake(100, 100)];
//    [path closePath];


    path.lineWidth = 8;
    [[UIColor redColor] setStroke];
    [path stroke];
    [[UIColor greenColor] setFill];
    [path fill];
}
  • 畫曲線
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(140, 40)];
    //添加兩個控制點 和 終點
    [path addCurveToPoint:CGPointMake(40, 180) controlPoint1:CGPointMake(40, 40) controlPoint2:CGPointMake(140, 180)];

    [path addCurveToPoint:CGPointMake(140, 320) controlPoint1:CGPointMake(140, 180) controlPoint2:CGPointMake(40, 320)];

    [[UIColor redColor]setStroke];
    [path stroke];
  • 畫矩形
    UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 80)];
    rectPath.lineWidth = 5;
    [[UIColor greenColor] setStroke];
    [rectPath stroke];
  • 繪制圓角矩形
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 150, 200, 80) cornerRadius:20];
    [roundedRect stroke];
  • 繪制橢圓
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 250, 200, 80)];
    [oval stroke];
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容