項(xiàng)目告一段落推沸。閑下來自己看看了貝塞爾進(jìn)行圖形繪制备绽,項(xiàng)目中沒有過太多,但是看一個(gè)技術(shù)群討論過繪圖鬓催,自己在網(wǎng)上看了許多肺素,學(xué)習(xí)了一下。
寫個(gè)類繼承UIView 我的JYJ_BezierPathView : UIView
然后寫個(gè)初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
繪圖需要寫在這個(gè)方法里- (void)drawRect:(CGRect)rect
- (void)drawRect:(CGRect)rect {
[self demo1];
//[self demo2];
//[self demo3];
//[self demo4];
//[self demo5];
//[self demo6];
}
demo1
代碼
- (void)demo1 {
// 初始化一個(gè)UIBezierPath對(duì)象.
UIBezierPath *bPath = [UIBezierPath bezierPath];
// 線寬.
bPath.lineWidth = 5;
// 拐點(diǎn)處理.
bPath.lineCapStyle = kCGLineCapRound;
// 終點(diǎn)處理.
bPath.lineJoinStyle = kCGLineCapRound;
// 添加線上的點(diǎn).
[bPath moveToPoint:CGPointMake(w / 2, 0.0)];
[bPath addLineToPoint:CGPointMake(w, h / 2)];
[bPath addLineToPoint:CGPointMake(w / 2, h)];
[bPath addLineToPoint:CGPointMake(0.0, h / 2)];
UIColor *fillColor = [UIColor redColor];
//繪制線線的里面的顏色
[fillColor setFill];
UIColor *strokeColor = [UIColor cyanColor];
//繪制線的顏色
[strokeColor setStroke];
[bPath closePath];
// 填充內(nèi)部顏色.
[bPath fill];
// 繪制線.
[bPath stroke];
}
demo2
代碼
- (void)demo2 {
//創(chuàng)建矩形
UIBezierPath *bPath = [UIBezierPath bezierPathWithRect:CGRectMake(30, 30, w - 60, h - 60)];
bPath.lineWidth = 10;
UIColor *stokeColor = [UIColor redColor];
[stokeColor setStroke];
//繪制線
[bPath stroke];
}
demo3
代碼
- (void)demo3 {
///這個(gè)芳法宇驾,是做一個(gè)內(nèi)切的曲線
// 圓形就是寬高相等
UIBezierPath *bPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, w - 10, w - 10)];
UIColor *stokeColor = [UIColor redColor];
[stokeColor setStroke];
bPath.lineWidth = 10;
[bPath stroke];
}
demo4
代碼
- (void)demo4 {
//繪制一條弧線 用 bezierPathWithArcCenter 這個(gè)方法
// 看下各個(gè)參數(shù)的意義:
// center:圓心的坐標(biāo)
// radius:半徑
// startAngle:起始的弧度 從開始的角度
// endAngle:圓弧結(jié)束的弧度 從結(jié)束的角度算
// clockwise:YES為順時(shí)針压怠,No為逆時(shí)針
UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(w / 2, h / 2) radius:130 startAngle: M_PI / 6 endAngle: M_PI / 6 * 5 clockwise:YES];
bPath.lineWidth = 5;
[bPath stroke];
}
demo5
代碼
- (void)demo5 {
//繪制二次貝塞爾曲線,moveToPoint,addQuadCurveToPoint這兩個(gè)搭配
// 二次貝塞爾的曲線支持
UIBezierPath *bPath = [UIBezierPath bezierPath];
// 開始的點(diǎn)
[bPath moveToPoint:CGPointMake(0, h)];
//終止點(diǎn)CurveToPoint飞苇,控制點(diǎn)controlPoint
[bPath addQuadCurveToPoint:CGPointMake(w, h) controlPoint:CGPointMake(w / 2, 0)];
[bPath fill];
}
demo6
代碼
- (void)demo6 {
//三次貝塞爾曲線
UIBezierPath *bPath = [UIBezierPath bezierPath];
[bPath moveToPoint:CGPointMake(0, h / 2)];
[bPath addCurveToPoint:CGPointMake(w, h/2) controlPoint1:CGPointMake(w/2, 0) controlPoint2:CGPointMake(w/2, h)];
bPath.lineWidth = 10;
UIColor *strokeColor = [UIColor cyanColor];
[strokeColor setStroke];
[bPath stroke];
}
簡單系統(tǒng)的學(xué)習(xí)了一下,這也是從網(wǎng)上學(xué)到的蜗顽。項(xiàng)目中總歸是要碰到的布卡,不如早點(diǎn)學(xué)學(xué)。