iOS开发-音量振动条的实现

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

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

iOS开发-音量振动条的实现.gif

音量振动条如何实现?

利用CAReplicatorLayer实现

如何利用CAReplicatorLayer实现?

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 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.anchorPoint = CGPointMake(0.5, 1);
    layer.position = CGPointMake(15, bgView.bounds.size.height);
    layer.bounds = CGRectMake(0, 0, 30, 150);
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    [repL addSublayer:layer];
    
    // 2.创建动画
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.scale.y";
    anim.toValue = @0.1;
    anim.duration = 0.5;
    anim.repeatCount = MAXFLOAT;
    
    // 设置反转动画
    anim.autoreverses = YES;
    [layer addAnimation:anim forKey:nil];
    
    // 复制层中子层总数
    // instanceCount:表示复制层里面有多少个子层,包括原始层
    repL.instanceCount = 4;
    
    // 设置复制子层偏移量,不包括原始层,相对于原始层x偏移
    repL.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
    
    // 设置复制层动画延迟时间
    repL.instanceDelay = 0.1;
    
    // 如果设置了原始层背景色,就不需要设置这个属性
    repL.instanceColor = [UIColor greenColor].CGColor;
    
    repL.instanceGreenOffset = -0.3;
}
上一篇下一篇

猜你喜欢

热点阅读