iOS關(guān)于貝塞爾曲線的使用

由于本人比較懶,很少寫博客,由于一個朋友公司需要實現(xiàn)下面這個等級效果,自己也閑來無聊,就用UIBezierPath實現(xiàn)了,并做個記錄。直接上代碼吧

//創(chuàng)建貝塞爾路徑

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(0.5f*M_PI) endAngle:2.5f*M_PI clockwise:YES];

//添加背景圓環(huán)

CAShapeLayer *backLayer = [CAShapeLayer layer];

backLayer.frame = self.bounds;

backLayer.fillColor =? [[UIColor clearColor] CGColor];

backLayer.strokeColor? = [UIColor colorWithRed:26.0/255.0f green:126.0/255.0f blue:255.0/255.0f alpha:1].CGColor;

backLayer.lineWidth = _lineWidth;

backLayer.path = [path CGPath];

backLayer.strokeEnd = 1;

[self.layer addSublayer:backLayer];


//設(shè)置漸變顏色

CAGradientLayer *gradientLayer =? [CAGradientLayer layer];

gradientLayer.frame = self.bounds;

[gradientLayer setColors:[NSArray arrayWithObjects:(id)[RGB(63, 204, 132) CGColor],(id)[RGB(98, 254, 52) CGColor], nil]];

gradientLayer.startPoint = CGPointMake(0, 0);

gradientLayer.endPoint = CGPointMake(0, 1);

[gradientLayer setMask:_proLayer]; //用progressLayer來截取漸變層

[self.layer addSublayer:gradientLayer];


//添加火柴棍

-(void)initTheLines

{

float radius = (self.bounds.size.width-30)/2.0f;

float sRadius = (self.bounds.size.width-20)/2.0f;

float bRadius = self.bounds.size.width/2.0f;

UIBezierPath *path = [[UIBezierPath alloc] init];

for (int i=1; i<20; i++)

{

[path moveToPoint:CGPointMake(15+radius-radius*sinf(i*2*M_PI/20),15+radius+radius*cosf(i*2*M_PI/20))];

if (i==5 || i==10 || i==15 || i==20)

{

[path addLineToPoint:CGPointMake(bRadius-bRadius*sinf(i*2*M_PI/20), bRadius+bRadius*cosf(i*2*M_PI/20))];

}

else

{

[path addLineToPoint:CGPointMake(10+sRadius-sRadius*sinf(i*2*M_PI/20), 10+sRadius+sRadius*cosf(i*2*M_PI/20))];

}

}

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.strokeColor = [UIColor colorWithRed:26.0/255.0f green:126.0/255.0f blue:255.0/255.0f alpha:1].CGColor;

shapeLayer.fillColor = [UIColor clearColor].CGColor;

shapeLayer.lineWidth = 5;

shapeLayer.lineJoin = kCALineJoinRound;

shapeLayer.lineCap = kCALineCapRound;

shapeLayer.path = path.CGPath;

//add it to our view

[self.layer addSublayer:shapeLayer];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容