UIBezierPath ios版簡單應用

1、可以用來畫遮罩。自定義圓角

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];

view2.backgroundColor = [UIColor whiteColor];

view2.backgroundColor = [UIColor redColor];

[self.view addSubview:view2];

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(0, 100)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

maskLayer.frame = view2.bounds;

maskLayer.path = maskPath.CGPath;

view2.layer.mask = maskLayer;

2、繪制圖形

(1)、drawRect方面里面直接構建路徑然后? ? [aPath stroke]

-(void)drawRect:(CGRect)rect{

UIColor *color = [UIColor redColor];

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

UIColor *fillColor = [UIColor greenColor];

[fillColor setFill];

UIBezierPath* aPath = [UIBezierPath bezierPath];

aPath.lineWidth = 5.0;

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

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

// Set the starting point of the shape.

[aPath moveToPoint:CGPointMake(100.0, 0.0)];

// Draw the lines

[aPath addLineToPoint:CGPointMake(200.0, 40.0)];

[aPath addLineToPoint:CGPointMake(160, 140)];

[aPath addLineToPoint:CGPointMake(40.0, 140)];

[aPath addLineToPoint:CGPointMake(0.0, 40.0)];

[aPath closePath]; //第五條線通過調用closePath方法得到的

[aPath fill];

[aPath stroke]; //Draws line 根據坐標點連線

}

(2)、添加在view的layer層

UIBezierPath* aPath = [UIBezierPath bezierPath];

aPath.lineWidth = 5.0;

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

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

// Set the starting point of the shape.

[aPath moveToPoint:CGPointMake(100.0, 0.0)];

// Draw the lines

[aPath addLineToPoint:CGPointMake(200.0, 40.0)];

[aPath addLineToPoint:CGPointMake(160, 140)];

[aPath addLineToPoint:CGPointMake(40.0, 140)];

[aPath addLineToPoint:CGPointMake(0.0, 40.0)];

aPath.lineWidth = 2;

[aPath closePath]; //第五條線通過調用closePath方法得到的

[aPath fill];

[aPath stroke]; //Draws line 根據坐標點連線

CAShapeLayer *layer = [[CAShapeLayer alloc]init];

layer.path = aPath.CGPath;

layer.fillColor = [UIColor greenColor].CGColor;

layer.strokeColor = [UIColor redColor].CGColor;

[self.view.layer addSublayer:layer];

3、原生方法繪制圖形

//創建一個橢圓的貝塞爾曲線 半徑相等 就是圓了

UIBezierPath *mPath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(60, 60, 10, 10)];

[mPath1 fill];

//創建一個矩形的貝塞爾線

UIBezierPath *mPath2 = [UIBezierPath bezierPathWithRect:CGRectMake(70,70, 10, 10)];

[mPath2 stroke];

//創建一個圓弧 傳的弧度

UIBezierPath *mPath3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 100)

radius:10

startAngle:DEGREES_TO_RADIANS(0)

endAngle:DEGREES_TO_RADIANS(180)

clockwise:YES];

[mPath3 fill];

//創建一個 矩形的貝塞爾曲線, 帶圓角

UIBezierPath *mPath4 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(250, 15, 20, 20) cornerRadius:3];

[mPath4 fill];

//定義一個矩形 邊角會變成 設置的角度? 方位/角度大小

UIBezierPath *mPath5 =[UIBezierPath bezierPathWithRoundedRect:CGRectMake(250, 45, 40, 40)

byRoundingCorners:UIRectCornerTopLeft

cornerRadii:CGSizeMake(10, 10)];

[mPath5 fill];

//定義一個二級的賽貝爾曲線 重點|拐彎點

UIBezierPath *mPath6 = [UIBezierPath bezierPath];

[mPath6 moveToPoint:CGPointMake(10,260)];

[mPath6 addQuadCurveToPoint:CGPointMake(200,260) controlPoint:CGPointMake(85, 240)];

[mPath6 setLineWidth:3];

[mPath6 stroke];

//定義一個三級的賽貝爾曲線 終點|拐點1|拐點2

UIBezierPath *mPath7 = [UIBezierPath bezierPath];

[mPath7 moveToPoint:CGPointMake(10,290)];

[mPath7 addCurveToPoint:CGPointMake(300, 290)

controlPoint1:CGPointMake(50, 270)

controlPoint2:CGPointMake(140, 340)];

[mPath7 stroke];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容