• 粒子雪花效果


    粒子雪花效果

    效果

    源码

    https://github.com/YouXianMing/Animations

    //
    //  EmitterSnowController.m
    //  Animations
    //
    //  Created by YouXianMing on 15/12/21.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    #import "EmitterSnowController.h"
    
    @interface EmitterSnowController ()
    
    @property (nonatomic, strong) CALayer   *movedMask;
    
    @end
    
    @implementation EmitterSnowController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
    }
    
    - (void)setup {
    
        [super setup];
        
        self.backgroundView.backgroundColor = [UIColor blackColor];
        
        // 创建粒子Layer
        CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];
        
        // 粒子发射位置
        snowEmitter.emitterPosition = CGPointMake(120,0);
        
        // 发射源的尺寸大小
        snowEmitter.emitterSize  = self.contentView.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   = 2.f;
        snowflake.greenRange = 2.f;
        snowflake.blueRange  = 2.f;
        
        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.contentView.layer addSublayer:snowEmitter];
        
        // 形成遮罩
        UIImage *image      = [UIImage imageNamed:@"alpha"];
        _movedMask          = [CALayer layer];
        _movedMask.frame    = (CGRect){CGPointZero, image.size};
        _movedMask.contents = (__bridge id)(image.CGImage);
        _movedMask.position = self.contentView.center;
        snowEmitter.mask    = _movedMask;
        
        // 拖拽的View
        UIView *dragView = [[UIView alloc] initWithFrame:(CGRect){CGPointZero, image.size}];
        dragView.center  = self.contentView.center;
        [self.contentView addSubview:dragView];
        
        // 给dragView添加拖拽手势
        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [dragView addGestureRecognizer:recognizer];
    }
    
    - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
        
        // 拖拽
        CGPoint translation = [recognizer translationInView:self.contentView];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                             recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.contentView];
        
        // 关闭CoreAnimation实时动画绘制(核心)
        [CATransaction setDisableActions:YES];
        _movedMask.position = recognizer.view.center;
    }
    
    @end

    细节

  • 相关阅读:
    关于将so 打包入APK的问题
    求 在独立service 中 调用contentprovider的方法
    ndk 环境下 c版 md5
    请教大牛们一个问题
    编写 service 与导出 jar 时注意的问题
    引入已编译好的动态库
    PHP 日期格式说明
    Ocaml 插件
    【转】简单至极的 PHP 缓存类
    PHP mysqlnd cannot connect to MySQL 4.1+ using old authentication
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5062496.html
Copyright © 2020-2023  润新知