ios 動畫 利用CAGradientLayer實現動畫

來源:互聯網
上載者:User

標籤:har   lock   cat   hit   start   邊框   sha   cloc   技術分享   

先看下片

分析步驟:

1.  先畫一個方形的layer,如果:

代碼如下:

CAGradientLayer *colorLayer = [CAGradientLayer layer];    colorLayer.backgroundColor = [UIColor blueColor].CGColor;    colorLayer.frame    = (CGRect){CGPointZero, CGSizeMake(200, 200)};    colorLayer.position = self.view.center;    [self.view.layer addSublayer:colorLayer];        // 顏色分配    colorLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,                          (__bridge id)[UIColor whiteColor].CGColor,                          (__bridge id)[UIColor redColor].CGColor];    // 開始的位置 設定成超出介面這樣預設就沒有效果    colorLayer.locations  = @[@(-0.2), @(-0.1), @(0)];        // 起始點    colorLayer.startPoint = CGPointMake(0, 0);        // 結束點    colorLayer.endPoint   = CGPointMake(1, 0);

2.設定光柵滑動效果

代碼如下:

    CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"locations"];    // 起始位置 左側外邊    fadeAnim.fromValue = @[@(-0.2), @(-0.1), @(0)];    // 結束位置 右側外邊    fadeAnim.toValue   = @[@(1.0), @(1.1), @(1.2)];    fadeAnim.duration  = 1.5;    [colorLayer addAnimation:fadeAnim forKey:nil];

3.設定layer的mask遮罩,只顯示圓圈

 

   UIBezierPath* bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:80 startAngle:DEGREES(0) endAngle:DEGREES(360) clockwise:YES];    CAShapeLayer *layer = [CAShapeLayer layer];    layer.path =bezierPath.CGPath;    // 位置    layer.position = CGPointMake(102, 100);    // 填充顏色為透明    layer.fillColor = [UIColor clearColor].CGColor;    // 邊框顏色為紅色    layer.strokeColor = [UIColor redColor].CGColor;    // 設定遮罩,這就就顯示遮罩內容    colorLayer.mask = layer;

 這樣就完成最終效果了。

ios 動畫 利用CAGradientLayer實現動畫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.