美文网首页
粒子动画

粒子动画

作者: SoManyDumb | 来源:发表于2016-08-24 22:16 被阅读73次

粒子动画

效果:随机绘制一条路径,点击开始按钮,粒子动画

实现思路

1.搞个画板绘制路径,自定义view

2.给自定义view添加pan手势,和创建复制图层和圆形图层,只需要设置一次,在awakeFromNib方法中设置。


    // 添加pan手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

    [self addGestureRecognizer:pan];

    // 创建复制图层
    CAReplicatorLayer *repLayer = [CAReplicatorLayer layer];

    repLayer.frame = self.bounds;

    [self.layer addSublayer:repLayer];


    // 创建粒子图层
    CALayer *layer = [CALayer layer];

    layer.cornerRadius = 5;

    layer.frame = CGRectMake(-100, 10, 10, 10);

    layer.backgroundColor = [UIColor whiteColor].CGColor;

    [repLayer addSublayer:layer];

    _dotLayer = layer;

3.因为核心动画只能设置一个路径,因此只能创建一个路径,懒加载路径。

- (UIBezierPath *)path
{
    if (_path == nil) {
        _path = [UIBezierPath bezierPath];
    }

    return _path;
}

4.在一开始拖动的时候,保存路径起点,设置路径起点,拖动的时候每次添加线到某个点。


    CGPoint curP = [pan locationInView:self];
    if (pan.state == UIGestureRecognizerStateBegan) {

        _startP = curP;

        [self.path moveToPoint:_startP];

    }

    [self.path addLineToPoint:curP];


    [self setNeedsDisplay];

5.路径绘制好了,点击开始按钮的时候,添加动画到图层

CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

    anim.keyPath = @"position";
    anim.duration = 4;
    anim.path = self.path.CGPath;

    anim.repeatCount = MAXFLOAT;

    [_dotLayer addAnimation:anim forKey:@"anim"];
    anim.delegate = self;

6.复制图层

    repLayer.instanceCount = 20;
    repLayer.instanceDelay = 4 / 20.0;

    // 设置子层颜色
    repLayer.instanceColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:1].CGColor;

    // 设置子层颜色绿色通道偏移量
    repLayer.instanceGreenOffset = -0.03;

7.重绘
清空路径,重新绘制,移除图层动画。

    _path = nil;
    [_dotLayer removeAnimationForKey:@"anim"];
    [self setNeedsDisplay];

相关文章

  • 粒子动画效果实现和Swift面向协议开发

    粒子动画效果实现是通过CALayer的子类,CAEmitterLayer实现。 实现粒子动画效果分以下几个步骤: ...

  • 粒子动画

  • 粒子动画

    占位符

  • 粒子动画

    一、在 UIKit 中,粒子系统由两部分组成: 1: 一个或多个 CAEmitterCells :发射器电...

  • 粒子动画

    一个或多个CAEmitterCells:发射器电池可以看作是单个粒子的原型(例如,一个单一的粉扑在一团烟雾)。当散...

  • 粒子动画

    @property(nonatomic, strong) CAEmitterLayer *emitterLayer...

  • 粒子动画

    公司做了一个类似小游戏的项目,背景需要放个流星雨效果粒子动画,详细的不多说,主要自己纪录学习,上代码。 -(voi...

  • 粒子动画

    粒子系统 直播中常用的粒子是怎么搞出来的呢,下面来进行一下探究 CAEmitterLayer 是一个高性能的粒子引...

  • 粒子动画

    什么是粒子系统? 粒子系统是由总体具有相同的表现规律,个体却随机表现出不同的特征的大量显示元素构成的集合 粒子定义...

  • 粒子动画

网友评论

      本文标题:粒子动画

      本文链接:https://www.haomeiwen.com/subject/zcmosttx.html