iOS CAShapeLayer 使用

CAShapeLayer介紹

  1. CAShapeLayer繼承自CALayer,可使用CALayer的所有屬性
  2. CAShapeLayer需要和貝塞爾曲線配合使用才有意義。貝塞爾曲線可以為其提供形狀,而單獨(dú)使用CAShapeLayer是沒(méi)有任何意義的。
  3. 使用CAShapeLayer與貝塞爾曲線可以實(shí)現(xiàn)不在view的DrawRect方法中畫(huà)出一些想要的圖形

關(guān)于CAShapeLayer和DrawRect的比較

  • DrawRect:DrawRect屬于CoreGraphic框架,占用CPU,消耗性能大
  • CAShapeLayer:CAShapeLayer屬于CoreAnimation框架,通過(guò)GPU來(lái)渲染圖形,節(jié)省性能。動(dòng)畫(huà)渲染直接提交給手機(jī)GPU,不消耗內(nèi)存

CAShapeLayer使用

請(qǐng)下載Demo 運(yùn)行CAShapeLayerDemo 工程

  • 繪制特別的形狀

CAShapeLayer 有一個(gè)神奇的屬性 path 用這個(gè)屬性配合上 UIBezierPath 這個(gè)類(lèi)就可以達(dá)到超神的效果。

圖1.1

實(shí)現(xiàn)如圖1.1效果,需要?jiǎng)?chuàng)建一個(gè)UIBezierPath曲線,賦值給 CAShapeLayer 的path屬性。
實(shí)現(xiàn)代碼如下 ,圖中紅點(diǎn)表示 startPoint,endPoint,controlPoint1,controlPoint2,

    // 創(chuàng)建 path
    UIBezierPath *path = [[UIBezierPath alloc]init];
    [path moveToPoint:startPoint];
    [path addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
    // 創(chuàng)建 shapeLayer
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc]init];
    [self.view.layer addSublayer:shapeLayer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    shapeLayer.lineWidth = 5;

一些特殊的界面效果,也可以通過(guò)CAShapeLayer 繪制出來(lái) 如圖1.2

圖1.2
    CGSize finalSize = CGSizeMake(CGRectGetWidth(self.view.frame), 600);
    CGFloat layerHeight = finalSize.height * 0.2;
    CAShapeLayer *bottomCurveLayer = [[CAShapeLayer alloc]init];
    
    UIBezierPath *path = [[UIBezierPath alloc]init];
    [path moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];
    [path addLineToPoint:CGPointMake(0, finalSize.height - 1)];
    [path addLineToPoint:CGPointMake(finalSize.width, finalSize.height - 1)];
    [path addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];
    [path addQuadCurveToPoint:CGPointMake(0, finalSize.height - layerHeight) controlPoint:CGPointMake(finalSize.width / 2, (finalSize.height - layerHeight) - 40)];

    
    bottomCurveLayer.path = path.CGPath;
    bottomCurveLayer.fillColor = [UIColor orangeColor].CGColor;
    [self.view.layer addSublayer:bottomCurveLayer];
  • 繪制進(jìn)度條
    屏幕快照 2016-07-28 下午1.12.22.png

創(chuàng)建要展示的四個(gè)圖層

// 灰色虛線
@property (nonatomic, strong)CAShapeLayer *baseLayer;
// 彩色虛線
@property (nonatomic, strong)CAShapeLayer *shapeLayer;
// 外圈灰色大圓
@property (nonatomic, strong)CAShapeLayer *bigBaseLayer;
// 帶顏色的大圓
@property (nonatomic, strong)CAShapeLayer *bigShapeLayer;

設(shè)置path 給圖層,

- (void)layoutSubviews {
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = MIN(self.bounds.size.width, self.bounds.size.height)/2 - dashLayerMargin;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:- M_PI_2 endAngle: (M_PI * 2)- M_PI_2 clockwise:YES];
    
    CGFloat bigRadius = radius + 8;
    UIBezierPath *bigPath = [UIBezierPath bezierPathWithArcCenter:center radius:bigRadius startAngle:- M_PI_2 endAngle: (M_PI * 2)- M_PI_2 clockwise:YES];
    
    self.shapeLayer.path = path.CGPath;
    self.baseLayer.path = path.CGPath;
    
    self.bigBaseLayer.path = bigPath.CGPath;
    self.bigShapeLayer.path = bigPath.CGPath;
    
    [self createGradientLayer];

}

控制進(jìn)度 使用 layer的strokeEnd 屬性


- (void)setProgress:(CGFloat)progress {
    _progress = progress;

    self.shapeLayer.strokeEnd = progress;
    self.bigShapeLayer.strokeEnd = progress;

}

一些 CAShapeLayer 動(dòng)畫(huà)

這些動(dòng)畫(huà)都基于CAShapeLayer & UIBezierPath 實(shí)現(xiàn)各種形狀圖層,使用 CoreAnimation顯式動(dòng)畫(huà)控制圖層的變化。
動(dòng)畫(huà)實(shí)現(xiàn)的關(guān)鍵是將復(fù)雜任務(wù)拆分成多個(gè)可實(shí)現(xiàn)的簡(jiǎn)單任務(wù),然后選擇合適的方法實(shí)現(xiàn)

收錄下面這些動(dòng)畫(huà),可以在需要時(shí)為動(dòng)畫(huà)實(shí)現(xiàn)提供參考,這里不做贅述。,詳見(jiàn)Demo中的
CAShapeLayerDemo
OneLoadingAnimation ,
ChangeAnimation

EyeRefreshView
drawing
彈簧效果

http://m.itdecent.cn/p/ce4e5f226d34

加載動(dòng)畫(huà)

http://m.itdecent.cn/p/1e2b8ff3519e

切換動(dòng)畫(huà)

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 目錄: 主要繪圖框架介紹 CALayer 繪圖 貝塞爾曲線-UIBezierPath CALayer子類(lèi) 補(bǔ)充:i...
    Ryan___閱讀 1,790評(píng)論 1 9
  • 書(shū)寫(xiě)的很好,翻譯的也棒!感謝譯者,感謝感謝! iOS-Core-Animation-Advanced-Techni...
    錢(qián)噓噓閱讀 2,444評(píng)論 0 6
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫(huà)效果,實(shí)現(xiàn)這些動(dòng)畫(huà)的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫(huà)全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,698評(píng)論 6 30
  • 幾次看到很火的一篇文章:《你無(wú)趣是因?yàn)槿鄙僖环N儀式感》。鏈接發(fā)給D先森想要他看,可是心里也知道一定又是點(diǎn)都懶得點(diǎn)開(kāi)...
    愛(ài)吃飯的小花閱讀 737評(píng)論 0 1
  • 不知不覺(jué)暑假已過(guò)了一大半,這一個(gè)多月里,我經(jīng)歷了很多同齡女生可能很難接觸到的東西,抑或明白了她們未嘗發(fā)覺(jué)的快樂(lè)。偶...
    若潯wfsindy閱讀 1,911評(píng)論 6 2

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