iOS 雪花动画与跑马灯

作者: 夏天然后 | 来源:发表于2016-07-22 15:09 被阅读0次

    这篇是接着上一篇, 关于动画效果的收集篇, 这篇介绍了跑马灯动画以及下落雪花动画, 请看, 话说最近怎么不在状态呢, 好伤感(囧~).
    上一篇 - iOS 仿YY直播心形动画 & 烟花动画

    跑马灯效果演示

    这里贴出使用代码, 详细请下载Demo查看

    下载即用~ 快餐 - -Star鼓励

    演示

    下载后, 在VC中这样使用, 当然Demo中也有体现

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    XTScrollLabelView *drawMarqueeView  = [[XTScrollLabelView alloc] initWithFrame:CGRectMake(0, 0, 250.f, 20)];
    drawMarqueeView.delegate          = self;
    drawMarqueeView.marqueeDirection  = FromLeftType;
    drawMarqueeView.center            = self.view.center;
    [self.view addSubview:drawMarqueeView];
    [drawMarqueeView addContentView:[self createLabelWithText:@"夏天是个很好的季节, 而夏天然后是简书的推荐作者, 喜欢分享!"
    textColor:[self randomColor]]];
    [drawMarqueeView startAnimation];
    
    }
    
    - (UILabel *)createLabelWithText:(NSString *)text textColor:(UIColor *)textColor {
    
    NSString *string = [NSString stringWithFormat:@" %@ ", text];
    CGFloat width = [string widthWithStringAttribute:@{NSFontAttributeName : [UIFont systemFontOfSize:14.f]}];
    UILabel  *label  = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 20)];
    label.font       = [UIFont systemFontOfSize:14.f];
    label.text       = string;
    label.textColor  = textColor;
    
    return label;
    }
    - (UIColor *)randomColor {
    
    return [UIColor colorWithRed:[self randomValue] green:[self randomValue] blue:[self randomValue] alpha:1];
    }
    - (CGFloat)randomValue {
    
    return arc4random() % 256 / 255.f;
    }
    - (void)drawMarqueeView:(XTScrollLabelView *)drawMarqueeView animationDidStopFinished:(BOOL)finished
    {
    [drawMarqueeView stopAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [drawMarqueeView addContentView:[self createLabelWithText:[self randomString]
    textColor:[self randomColor]]];
    [drawMarqueeView startAnimation];
    });
    }
    - (NSString *)randomString {
    
    NSArray *array = @[@"人帅",
    @"勤劳",
    @"年轻",
    @"刻苦",
    @"开玩笑",
    @"都是我编的, 前面的别跑"];
    return array[arc4random() % array.count];
    }
    
    
    演示

    </div>

    雪花动画代码演示

    -- 在ViewDidLoad中这样写, 注释已经很详细

    // 创建粒子Layer
    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];
    
    // 粒子发射位置
    snowEmitter.emitterPosition = CGPointMake(120,0);
    
    // 发射源的尺寸大小
    snowEmitter.emitterSize     = self.view.bounds.size;
    
    // 发射模式
    snowEmitter.emitterMode     = kCAEmitterLayerSurface;
    
    // 发射源的形状
    snowEmitter.emitterShape    = kCAEmitterLayerLine;
    
    // 创建雪花类型的粒子
    CAEmitterCell *snowflake    = [CAEmitterCell emitterCell];
    
    // 粒子的名字
    snowflake.name = @"snow";
    
    // 粒子参数的速度乘数因子
    snowflake.birthRate = 20.0;
    snowflake.lifetime  = 120.0;
    
    // 粒子速度
    snowflake.velocity  = 10.0;
    
    // 粒子的速度范围
    snowflake.velocityRange = 10;
    
    // 粒子y方向的加速度分量
    snowflake.yAcceleration = 2;
    
    // 周围发射角度
    snowflake.emissionRange = 0.5 * M_PI;
    
    // 子旋转角度范围
    snowflake.spinRange = 0.25 * M_PI;
    snowflake.contents  = (id)[[UIImage imageNamed:@"snow"] CGImage];
    
    // 设置雪花形状的粒子的颜色
    snowflake.color      = [[UIColor whiteColor] CGColor];
    snowflake.redRange   = 1.5f;
    snowflake.greenRange = 2.2f;
    snowflake.blueRange  = 2.2f;
    
    snowflake.scaleRange = 0.6f;
    snowflake.scale      = 0.7f;
    
    snowEmitter.shadowOpacity = 1.0;
    snowEmitter.shadowRadius  = 0.0;
    snowEmitter.shadowOffset  = CGSizeMake(0.0, 0.0);
    
    // 粒子边缘的颜色
    snowEmitter.shadowColor  = [[UIColor whiteColor] CGColor];
    
    // 添加粒子
    snowEmitter.emitterCells = @[snowflake];
    
    // 将粒子Layer添加进图层中
    [self.view.layer addSublayer:snowEmitter];
    
    // 形成遮罩
    UIImage *image      = [UIImage imageNamed:@"alpha"];
    _layer              = [CALayer layer];
    _layer.frame        = (CGRect){CGPointZero, self.view.bounds.size};
    _layer.contents     = (__bridge id)(image.CGImage);
    _layer.position     = self.view.center;
    snowEmitter.mask    = _layer;
    
    pic

    相关文章

      网友评论

        本文标题:iOS 雪花动画与跑马灯

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