形狀圖層和復制圖層的簡單搭配使用


// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end


// ViewController.m
#import "ViewController.h"
#import "UIView+Frame.h"

//屏幕寬高
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

/*
    主要實現(xiàn)思路:
    1.創(chuàng)建形狀圖層
    2.創(chuàng)建復制圖層
    3.創(chuàng)建透明度動畫
    4.創(chuàng)建縮放動畫
    5.創(chuàng)建動畫組把2個動畫添加進去
    6.把動畫組添加到形狀圖層
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUp];
}

- (void)setUp {

    UIView *animBack = [[UIView alloc] init];
    animBack.width = screenW;
    animBack.height = screenW;
    animBack.center = self.view.center;
    [self.view addSubview:animBack];

    // 形狀圖層
    CAShapeLayer *shapL = [CAShapeLayer layer];
    shapL.frame = animBack.bounds;
    shapL.fillColor = [UIColor redColor].CGColor;
    shapL.path = [UIBezierPath bezierPathWithOvalInRect:shapL.bounds].CGPath;
//    shapL.path = [UIBezierPath bezierPathWithRect:shapL.bounds].CGPath;
    // 這句代碼不加的話,程序一起來,就會出現(xiàn)一個很大的紅色的圓
    shapL.opacity = 0;

    // 復制圖層
    CAReplicatorLayer *repL = [CAReplicatorLayer layer];
    repL.frame = animBack.bounds;
    // 復制的份數(shù)算自己本身一份
    repL.instanceCount = 3;
    // 每一份顯示的間隔時間
    repL.instanceDelay = 0.5;
    [animBack.layer addSublayer:repL];
    // 把形狀圖層添加到復制層中去
    [repL addSublayer:shapL];

    // 添加透明動畫
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    anim.fromValue = @(0.3);
    anim.toValue = @(0);

    // 添加放大動畫
    CABasicAnimation *anim2  = [CABasicAnimation animationWithKeyPath:@"transform"];
    anim2.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 0)];
    anim2.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 0)];

    // 創(chuàng)建動畫組
    CAAnimationGroup *groupA = [CAAnimationGroup animation];
    groupA.animations = @[anim, anim2];
    groupA.duration = 3.0;
    groupA.repeatCount = HUGE;
    // 給形狀圖層添加動畫
    [shapL addAnimation:groupA forKey:nil];
}

@end

  • 實現(xiàn)效果:
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 窗外稀里嘩啦地響,北風攜卷著細碎的雪花,整整刮了一夜。 五點多鐘,許阿姨利落地從被窩里起身,簡單洗漱后,來到廚房做...
    俞壹閱讀 288評論 5 8
  • 周六上午約了一個跑步的培訓,打算參加完培訓,就找個咖啡館看看書學學習。背上書包,書包里面放了電腦和書籍。集合的地點...
    小花Ivan閱讀 971評論 0 1
  • 1.把車貸還了 2.晚上遛狗時跑步 3.讀書30-50本 4.盡量找對象 5.自學英文,西語 6.減肥15kg
    我真的要當作家閱讀 424評論 1 50
  • 樂吃瘦法,目前在減肥領域最先進的一種減肥方法,也是最健康、最輕松的減肥方法! 樂吃瘦法,和傳統(tǒng)的方法不同的是杜絕了...
    熱門小東東閱讀 454評論 0 0