畫圓角及邊框

1.方式一:

最常見(jiàn)設(shè)計(jì)圓角,繪制邊框的方法是利用CALayercornerRadiusborderWidth, borderColor來(lái)實(shí)現(xiàn):

self.view.layer.maskToBounds = YES;
self.view.layer.cornerRadius = 4.f;
self.view.layer.borderWidth = 2.f;
self.view.layer.borderColor = [UIColor redColor].CGColor;

這個(gè)實(shí)現(xiàn)方法里maskToBounds會(huì)觸發(fā)離屏渲染(offscreen rendering),會(huì)導(dǎo)致appFPS下降,特別是給UICollectionViewCell設(shè)計(jì)圓角的時(shí)候,用戶滑動(dòng)瀏覽時(shí),會(huì)覺(jué)得明顯的卡頓,用戶體驗(yàn)非常不好,所以在給多個(gè)view設(shè)計(jì)圓角的時(shí)候不建議使用。

2.方式二:

直接上代碼

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell isKindOfClass:[GXSectionCell class]]) {
        CGFloat cellWidth = cell.width;
        CGFloat cellHeight = cell.height;
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = CGRectMake(0, 0, cellWidth, cellHeight);
        
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.frame = CGRectMake(0, 0, cellWidth, cellHeight);
        borderLayer.lineWidth = 1.f;
        borderLayer.strokeColor = [UIColor colorWithHexString:@"D2D2D2"].CGColor;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(KNormalSpace, KNormalSpace/4, cellWidth - 2 * KNormalSpace, cellHeight - KNormalSpace/2) cornerRadius:5.0f];
        maskLayer.path = bezierPath.CGPath;
        borderLayer.path = bezierPath.CGPath;
        
        [cell.contentView.layer insertSublayer:borderLayer atIndex:0];
        [cell.layer setMask:maskLayer];
    }
}
  • 代碼中創(chuàng)建了兩個(gè)CAShapeLayer, 關(guān)于CAShapeLayer先講幾句,CAShapeLayer屬于Core Animation框架,會(huì)通過(guò)GPU來(lái)渲染圖形,節(jié)省性能,動(dòng)畫渲染直接交給手機(jī)GPU,不消耗內(nèi)存。CAShapeLayershape是形狀的意思,需要形狀才能生效,而貝塞爾曲線恰恰可以為CAShapeLayer提供路徑,CAShapeLayer在提供的路徑中進(jìn)行渲染繪制出了shape
  • 先看代碼中創(chuàng)建的貝塞爾曲線,通過(guò)UIBezierPathbezierPathWithRoundedRect:cornerRadius:方法來(lái)繪制出一個(gè)帶圓角矩形的路徑,用來(lái)給CAShapeLayer繪制圖形
  • 第一個(gè)layer叫做maskLayer,是用來(lái)給cell實(shí)現(xiàn)遮罩效果,因?yàn)?code>layer使用的path是一個(gè)圓角矩形的貝塞爾曲線,那么這個(gè)layer形成的遮罩可以實(shí)現(xiàn)cell的圓角。
  • 第二個(gè)layer叫做borderLayer,是用來(lái)繪制cell邊框顏色的,通過(guò)CAShapeLayerlineWidth, strokeColor來(lái)繪制邊框,注意fillColor一定要設(shè)置成clearColor,否則會(huì)擋住整個(gè)cell
  • 最后,將borderLayer加載到celllayer上,將maskLayer設(shè)置成cell layermask。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,674評(píng)論 1 32
  • 在最新的版本迭代里,UI想給UICollectionViewCell切成圓角并且要有色邊框,以那種卡片形式展現(xiàn)給用...
    Kobe_Dai閱讀 11,309評(píng)論 9 63
  • 今天就來(lái)說(shuō)說(shuō)iOS最常用的控件-UITableView,基本用法就不多說(shuō)了,大家應(yīng)該都知道,當(dāng)然對(duì)于它的優(yōu)化大家也...
    sumrain_cloud閱讀 381評(píng)論 0 1
  • 1 NSOperationQueue和GCD的區(qū)別是什么 GCD(Grand Central Dispatch)是...
    紫色冰雨閱讀 798評(píng)論 0 2
  • 前言 說(shuō)起優(yōu)化,簡(jiǎn)直是博大精深。話不多說(shuō),筆者今天梳理的內(nèi)容,UITableView的性能優(yōu)化。先說(shuō)一下table...
    陌路賣醬油閱讀 9,977評(píng)論 0 57

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