iOS树叶进度效果

分析图.png

效果:小风车不停的转动,进度增加期间没被填满的地方有树叶飘过

小风车动画

使用基础动画让他无限旋转

    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = @1;
    scaleAnimation.toValue = @0;
    scaleAnimation.duration = 0.5;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fillMode = kCAFillModeForwards;
    [self.flowerView.layer addAnimation:scaleAnimation forKey:nil];
    
    _textLabel.hidden = NO;
    scaleAnimation.fromValue = @0;
    scaleAnimation.toValue = @1;
    [_textLabel.layer addAnimation:scaleAnimation forKey:nil];

树叶移动动画

1.每次事件触发”下一层的视图“在他的layer上面添加子layer

    //树叶效果(用CALayer创建枫叶)
    CALayer *leafLayer = [CALayer layer];
    leafLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"leaf"].CGImage);
    leafLayer.bounds = CGRectMake(0, 0, 10, 10);
    leafLayer.position = CGPointMake(_progressBGImageView.frame.origin.x + _progressBGImageView.frame.size.width, 8);
    [self.progressBGImageView.layer addSublayer:leafLayer];

2.使用动画组,将路径和旋转动画结合播放

    //添加路径
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSValue *p1 = [NSValue valueWithCGPoint:leafLayer.position];
    NSValue *p2 = [NSValue valueWithCGPoint:CGPointMake(_progressBGImageView.frame.origin.x + _progressBGImageView.frame.size.width*3/4.0 + arc4random()%(int)(_progressBGImageView.frame.size.width/4.0), _progressBGImageView.frame.origin.y + arc4random()%(int)_progressBGImageView.frame.size.height)];
    NSValue *p3 = [NSValue valueWithCGPoint:CGPointMake(_progressBGImageView.frame.origin.x + _progressBGImageView.frame.size.width/2.0 + arc4random()%(int)(_progressBGImageView.frame.size.width/4.0), _progressBGImageView.frame.origin.y + arc4random()%(int)_progressBGImageView.frame.size.height)];
    NSValue *p4 = [NSValue valueWithCGPoint:CGPointMake(_progressBGImageView.frame.origin.x + _progressBGImageView.frame.size.width/4.0 + arc4random()%(int)(_progressBGImageView.frame.size.width/4.0), _progressBGImageView.frame.origin.y + arc4random()%(int)_progressBGImageView.frame.size.height)];
    NSValue *p5 = [NSValue valueWithCGPoint:CGPointMake(_progressBGImageView.frame.origin.x+5, _progressBGImageView.frame.origin.y + arc4random()%(int)_progressBGImageView.frame.size.height)];
    
    keyAnimation.values = @[p1, p2, p3, p4, p5];
    
    //设置旋转动画
    CABasicAnimation *roateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    roateAnimation.fromValue = @0;
    roateAnimation.toValue = @(M_PI/18.0 * (arc4random()%(36*6) ));
    
    
    //动画组(播放旋转动画和路径)
    CAAnimationGroup  *group = [CAAnimationGroup animation];
    group.animations = @[roateAnimation, keyAnimation];
    group.duration = 8;
    group.delegate = self;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    [group setValue:leafLayer forKey:@"leafLayer"];
    [leafLayer addAnimation:group forKey:nil];

3.通过动画的代理,在动画完成之后删除添加的layer

//动画的代理

#pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    //移除layer的动画
    CALayer *layer = [anim valueForKey:@"leafLayer"];
    [layer removeFromSuperlayer];
}

下载链接

http://code.cocoachina.com/view/129385

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,586评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,152评论 5 13
  • 我是日记星球239号星宝宝,我叫叶子。我参加了日记星球第五期21天的蜕变之旅,虽然课程已经结束,不过我学习的脚步不...
    水晶妈咪阅读 351评论 0 0
  • 运行时:1 会用来开发oc的2如果在程序开发中,有一些动态的需求,通常可以使用运行时来处理关于运行时。 总共有三种...
    BeijingIamback阅读 623评论 0 4
  • 人说山西好风光第二季,运城的朗诵荡气回肠,豪情顿生。
    张开笑脸沐浴霞光阅读 125评论 0 0