iOS開發(fā) 利用UIBezierPath畫弧

需要實(shí)現(xiàn)效果


image.png

方案1:利用方法

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise API_AVAILABLE(ios(4.0));

參數(shù)詳解:
center:弧形的圓心
radius:半徑
startAngle:開始角度
endAngle:結(jié)束角度
clockwise:是否是順時(shí)針方向

使用:

    // 畫圓弧
    CGFloat kViewHeight = 200;
    UIView *showView = [[UIView alloc] initWithFrame:(CGRectMake(0, self.view.frame.size.height-kViewHeight, self.view.frame.size.width, kViewHeight))];
    showView.backgroundColor = UIColor.grayColor;
    [self.view addSubview:showView];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    //畫圓弧 順時(shí)針半圈
    CGFloat kRadius = kViewHeight*3;
    [bezierPath addArcWithCenter:CGPointMake(self.view.frame.size.width/2, kRadius) radius:kRadius startAngle:0 endAngle:-M_PI clockwise:NO];
    [bezierPath closePath];
    //  設(shè)置顏色(顏色設(shè)置也可以放在最上面,只要在繪制前都可以)
    [UIColor.redColor setStroke];
    [UIColor.systemPinkColor setFill];
    // 描邊和填充
    [bezierPath stroke];
    [bezierPath fill];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    layer.strokeColor = UIColor.redColor.CGColor;
    layer.fillColor = UIColor.greenColor.CGColor;
    layer.backgroundColor = UIColor.systemPinkColor.CGColor;
    [showView.layer addSublayer:layer];

展現(xiàn)效果


image.png

這種方案實(shí)現(xiàn)的優(yōu)點(diǎn)是方便理解原理,弧度的大小可以直接由半徑來決定,缺點(diǎn)是位置控制起來不是很方便植影,圓心還要下移才能固定弧度所在的位置箱锐。

方案二:
這個(gè)是我選擇使用的方案硫椰。

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

參數(shù)詳解:
endPoint:結(jié)束點(diǎn)
controlPoint:控制點(diǎn)
我不太會描述制跟,直接用網(wǎng)上一張圖來描述的controlPoint的作用

image.png

看見圖你會發(fā)現(xiàn),A點(diǎn)是現(xiàn)在的點(diǎn)仪召,C點(diǎn)就是endPoint寨蹋,也即是結(jié)束的點(diǎn)。

QQ20200924-114045.png

經(jīng)過我不確定是否準(zhǔn)確的測試扔茅,弧形的頂點(diǎn)P大致為B點(diǎn)到直線AC的垂點(diǎn)M的中心點(diǎn)已旧。也就是說,P點(diǎn)為直線BM的中心點(diǎn)召娜。當(dāng)然运褪,也有可能P點(diǎn)不在BM線上,所以準(zhǔn)確點(diǎn)說就是玖瘸,PBM的垂直交點(diǎn)就是BM的中心點(diǎn)秸讹。

系統(tǒng)提供的該方法中,BC點(diǎn)都有了但是沒有A點(diǎn)雅倒,所以我們在描繪之前要先移動到A點(diǎn)

[bezierPath moveToPoint: CGPointMake(0, kRadian)]

實(shí)現(xiàn)代碼:

    self.view.backgroundColor = UIColor.systemGray5Color;
    // 畫圓弧
    CGFloat kViewHeight = 200;
    UIView *showView = [[UIView alloc] initWithFrame:(CGRectMake(0, self.view.frame.size.height-kViewHeight, self.view.frame.size.width, kViewHeight))];
    showView.backgroundColor = UIColor.grayColor;
    [self.view addSubview:showView];
    
    CGFloat kRadian = 35;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(0, kRadian)];
    [bezierPath addQuadCurveToPoint:CGPointMake(self.view.frame.size.width, kRadian) controlPoint:CGPointMake(self.view.frame.size.width/2, -kRadian)];
    [bezierPath addLineToPoint: CGPointMake(self.view.frame.size.width, showView.frame.size.height)];
    [bezierPath addLineToPoint: CGPointMake(0, showView.frame.size.height)];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    layer.strokeColor = UIColor.clearColor.CGColor;
    layer.fillColor = UIColor.whiteColor.CGColor;
    [showView.layer addSublayer:layer];

實(shí)現(xiàn)效果:


image.png

這種方案實(shí)現(xiàn)的優(yōu)點(diǎn)是 璃诀,弧度的大小可以直接由controlPoint來決定。并且可以通過addLineToPoint來把自己需要的范圍圈起來蔑匣。

我們可以給layer再增加一些陰影效果劣欢,然后把showView 的背景色去掉:

    self.view.backgroundColor = UIColor.systemGray6Color;
    // 畫圓弧
    CGFloat kViewHeight = 200;
    UIView *showView = [[UIView alloc] initWithFrame:(CGRectMake(0, self.view.frame.size.height-kViewHeight, self.view.frame.size.width, kViewHeight))];
    showView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:showView];
    
    CGFloat kRadian = 35;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(0, kRadian)];
    [bezierPath addQuadCurveToPoint:CGPointMake(self.view.frame.size.width, kRadian) controlPoint:CGPointMake(self.view.frame.size.width/2, -kRadian)];
    [bezierPath addLineToPoint: CGPointMake(self.view.frame.size.width, showView.frame.size.height)];
    [bezierPath addLineToPoint: CGPointMake(0, showView.frame.size.height)];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    layer.strokeColor = UIColor.clearColor.CGColor;
    layer.fillColor = UIColor.whiteColor.CGColor;
    layer.cornerRadius = 3.0;
    layer.masksToBounds = NO;
    layer.shadowOffset = CGSizeMake(-5, -5); //(0,0)時(shí)是四周都有陰影
    layer.shadowColor = [UIColor grayColor].CGColor;
    layer.shadowOpacity = 0.1;
    [showView.layer addSublayer:layer];

大功告成


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市裁良,隨后出現(xiàn)的幾起案子凿将,更是在濱河造成了極大的恐慌,老刑警劉巖价脾,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件牧抵,死亡現(xiàn)場離奇詭異,居然都是意外死亡侨把,警方通過查閱死者的電腦和手機(jī)犀变,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來座硕,“玉大人弛作,你說我怎么就攤上這事』遥” “怎么了映琳?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蜘拉。 經(jīng)常有香客問我萨西,道長,這世上最難降的妖魔是什么旭旭? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任谎脯,我火速辦了婚禮,結(jié)果婚禮上持寄,老公的妹妹穿的比我還像新娘源梭。我一直安慰自己娱俺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布废麻。 她就那樣靜靜地躺著荠卷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪烛愧。 梳的紋絲不亂的頭發(fā)上油宜,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音怜姿,去河邊找鬼慎冤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛沧卢,可吹牛的內(nèi)容都是我干的蚁堤。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼搏恤,長吁一口氣:“原來是場噩夢啊……” “哼违寿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起熟空,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤藤巢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后息罗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掂咒,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年迈喉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了绍刮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡挨摸,死狀恐怖孩革,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情得运,我是刑警寧澤膝蜈,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站熔掺,受9級特大地震影響饱搏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜置逻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一推沸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦鬓催、人聲如沸肺素。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽压怠。三九已至,卻和暖如春飞苇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蜗顽。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工布卡, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人雇盖。 一個(gè)月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓忿等,卻偏偏與公主長得像,于是被迫代替她去往敵國和親崔挖。 傳聞我的和親對象是個(gè)殘疾皇子贸街,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354