iOS开发-活动指示器功能的实现

2018-10-11  本文已影响0人  iOS_ZZ

今天呢给同学们讲解一下通过核心动画实现一个活动指示器功能,那么废话不多说直接上代码~先看演示视频

iOS开发-活动指示器功能的实现.gif

活动指示器实现思路

// 0.创建背景view
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor grayColor];
    CGFloat bgViewX = self.view.frame.size.width * 0.5  ;
    CGFloat bgViewY = 300;
    CGFloat bgViewW = 200;
    CGFloat bgViewH = 200;
    bgView.center = CGPointMake(bgViewX, bgViewY);
    bgView.bounds = CGRectMake(0, 0, bgViewW, bgViewH);
    [self.view addSubview:bgView];
    
    // 1.创建复制图层 可以把图层里面所有子层复制
    CAReplicatorLayer *repL = [CAReplicatorLayer layer];
    repL.frame = bgView.bounds;
    [bgView.layer addSublayer:repL];
    CALayer *layer = [CALayer layer];
    layer.transform = CATransform3DMakeScale(0, 0, 0);
    layer.position = CGPointMake(bgView.bounds.size.width * 0.5, 20);
    layer.bounds = CGRectMake(0, 0, 10, 10);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    [repL addSublayer:layer];
    
    // 2.创建动画
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.scale";
    anim.fromValue = @1;
    anim.toValue = @0;
    CGFloat duration = 1;
    anim.duration = duration;
    anim.repeatCount = MAXFLOAT;
    [layer addAnimation:anim forKey:nil];
    int count = 20;
    CGFloat angle = M_PI * 2 / count;
    
    // 复制层中子层总数
    // instanceCount:表示复制层里面有多少个子层,包括原始层
    repL.instanceCount = count;
    
    // 设置复制子层偏移量,不包括原始层,相对于原始层x偏移
    repL.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
    // 设置复制层动画延迟时间
    repL.instanceDelay = duration / count;
上一篇 下一篇

猜你喜欢

热点阅读