粒子动画

实现方式

1背景图

 1>

      self.image = [UIImage imageNamed:@"bg1.png"];
        self.clipsToBounds = YES;
        self.layer.masksToBounds = YES;
2>光晕自传动画

// 无限自动旋转的光晕
- (void)animationHalo
{
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg2.png"]];
    imageView2.frame = self.bounds;
    imageView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imageView2.clipsToBounds = YES;
    [self addSubview:imageView2];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 200;
    animation.repeatCount = MAXFLOAT;
    animation.fromValue = [NSNumber numberWithDouble:0];
    animation.toValue = [NSNumber numberWithDouble:M_PI*2];
    [imageView2.layer addAnimation:animation forKey:@"transform"];
}

3>产生星星和点点的动画

// 自动生产的星星和点点的动画
- (void)animationAutoProductStarAndPoint
{
    CAEmitterLayer *emitterLayer = (CAEmitterLayer *)self.layer;
    emitterLayer.emitterPosition = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);    // 坐标
    emitterLayer.emitterSize = self.bounds.size;            // 粒子大小
    emitterLayer.renderMode = kCAEmitterLayerOldestLast;     // 递增渲染模式
    emitterLayer.emitterMode = kCAEmitterLayerSurface;      // 粒子发射模式(面发射)
	emitterLayer.emitterShape = kCAEmitterLayerSphere;      // 粒子形状(球状)
    emitterLayer.seed = (arc4random()%100)+1;   // 用于初始化随机数产生的种子

    // 点点粒子
    CAEmitterCell *cycleCell = [CAEmitterCell emitterCell];
    cycleCell.birthRate = 0.2;
    cycleCell.lifetime = MAXFLOAT;
    cycleCell.contents = (id)[[UIImage imageNamed:@"bgAnimationPoint1.png"] CGImage]; // cell内容,一般是一个CGImage
    cycleCell.color = [[UIColor whiteColor] CGColor];
    cycleCell.velocity = 10;        // 粒子发射速度
    cycleCell.velocityRange = 2;
    cycleCell.alphaRange = 0.5;
    cycleCell.alphaSpeed = 2;
    cycleCell.scale = 0.1;
    cycleCell.scaleRange = 0.1;
    [cycleCell setName:@"starPoint"];
    
    // 星星粒子
    CAEmitterCell *starCell = [CAEmitterCell emitterCell];
    starCell.birthRate = 2;
    starCell.lifetime = 2.02;
    
    CAEmitterCell *starCell0 = [CAEmitterCell emitterCell];
    starCell0.birthRate = 0.5;
    starCell0.lifetime = 2.02;
    starCell0.velocity = 0;
    starCell0.emissionRange = 2*M_PI;    // 发射角度范围
    starCell0.contents = (id)[[UIImage imageNamed:@"bgAnimationStar.png"] CGImage];
    starCell0.color = [[UIColor colorWithRed:1 green:1 blue:1 alpha:0] CGColor];
    starCell0.alphaSpeed = 0.6;
    starCell0.scale = 0.4;
    [starCell0 setName:@"star"];
    
    CAEmitterCell *starCell1 = [CAEmitterCell emitterCell];
    starCell1.birthRate = 0.5;
    starCell1.lifetime = 2.02;
    starCell1.velocity = 0;
    starCell1.emissionRange = 2*M_PI;    // 发射角度范围
    
    CAEmitterCell *starCell2 = [CAEmitterCell emitterCell];
    starCell2.birthRate = 0.5;
    starCell2.lifetime = 2;
    starCell2.velocity = 0;
    starCell2.emissionRange = 2*M_PI;    // 发射角度范围
    starCell2.contents = (id)[[UIImage imageNamed:@"bgAnimationStar.png"] CGImage];
    starCell2.color = [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor;
    starCell2.alphaSpeed = -0.5;
    starCell2.scale = starCell0.scale;
    
    
    emitterLayer.emitterCells = @[starCell];
    starCell.emitterCells = @[starCell0, starCell1];
    starCell1.emitterCells = @[starCell2];
}

2 移动的粒子束动画

1>产生粒子束

- (void)animationEmitter
{
    CAEmitterLayer *emitterLayer = (CAEmitterLayer *)self.layer;         
    emitterLayer.emitterPosition = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);    // 坐标
    emitterLayer.emitterSize = self.bounds.size;            // 粒子大小
    emitterLayer.renderMode = kCAEmitterLayerAdditive;      // 递增渲染模式
    emitterLayer.emitterMode = kCAEmitterLayerPoints;       // 粒子发射模式(面发射)
	emitterLayer.emitterShape = kCAEmitterLayerSphere;      // 粒子形状(球状)
    
    // 星星粒子
    CAEmitterCell *cell1 = [self productEmitterCellWithContents:(id)[[UIImage imageNamed:@"star1.png"] CGImage]];
    cell1.scale = 0.3;
    cell1.scaleRange = 0.1;
    
    // 圆粒子
    CAEmitterCell *cell2 = [self productEmitterCellWithContents:(id)[[UIImage imageNamed:@"cycle1.png"] CGImage]];
    cell2.scale = 0.05;
    cell2.scaleRange = 0.02;
    
    emitterLayer.emitterCells = @[cell1, cell2];
}

- (CAEmitterCell *)productEmitterCellWithContents:(id)contents
{
    CAEmitterCell *cell = [CAEmitterCell emitterCell];
    cell.birthRate = 120;       // 每秒产生粒子数
    cell.lifetime = 1;          // 每个粒子的生命周期
    cell.lifetimeRange = 0.3;
    cell.contents = contents;   // cell内容,一般是一个CGImage
    cell.color = [[UIColor whiteColor] CGColor];
    cell.velocity = 50;         // 粒子的发射方向
    cell.emissionLongitude = M_PI*2;
    cell.emissionRange = M_PI*2;
    cell.velocityRange = 10;
    cell.spin = 10;
    
    return cell;
}

2>根据CGPath生成关键帧动画

- (void)animationMoveWithPath:(CGPathRef)path
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"emitterPosition"];
    animation.path = path;
    animation.duration = 4;
    animation.repeatCount = MAXFLOAT;
    [self.layer addAnimation:animation forKey:nil];
}

3>放烟花动画

分步骤:爆炸前的移动星星粒子

 爆炸是的而粒子

 爆炸后散射的粒子 


- (void)animationEmitter
{
    CAEmitterLayer *emitterLayer = (CAEmitterLayer *)self.layer;
    emitterLayer.emitterPosition = CGPointMake(self.bounds.size.width/2, self.bounds.size.height);    // 坐标
    emitterLayer.emitterSize = CGSizeMake(self.bounds.size.width, 0);               // 粒子大小
    emitterLayer.renderMode = kCAEmitterLayerAdditive;      // 递增渲染模式
    emitterLayer.emitterMode = kCAEmitterLayerOutline;      // 粒子发射模式(向线外发射)
	emitterLayer.emitterShape = kCAEmitterLayerLine;        // 粒子形状(线)
    emitterLayer.seed = (arc4random()%100) + 1;
    
    // 爆炸前的移动星星圆粒子
    CAEmitterCell *cycleCell = [CAEmitterCell emitterCell];
    cycleCell.birthRate = 1;
    cycleCell.lifetime = 1.02;
    cycleCell.emissionLatitude = 0;
    cycleCell.emissionLongitude = 0;
    cycleCell.emissionRange = M_PI_4/2;    // 发射角度范围
    cycleCell.velocity = 200;
    cycleCell.contents = (id)[[UIImage imageNamed:@"cycle1.png"] CGImage];
    cycleCell.scale = 0.05;
    
    // 爆炸时的粒子
    CAEmitterCell *burstCell = [CAEmitterCell emitterCell];
	burstCell.birthRate	= cycleCell.birthRate;
    burstCell.scale = 2.5;
	burstCell.lifetime = 0.2;
    
    // 爆炸后的散射星星例子
    CAEmitterCell *starCell = [CAEmitterCell emitterCell];
    starCell.birthRate = 400;
    starCell.velocity = 100;
    starCell.lifetime = 1;
    starCell.lifetimeRange = 0.5;
    starCell.emissionRange = 2*M_PI;    // 发射角度范围
    starCell.yAcceleration = 75;
    starCell.contents = (id)[[UIImage imageNamed:@"star1.png"] CGImage];
    starCell.color = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1].CGColor;
    starCell.alphaSpeed = -0.8;
    starCell.scale = 2;
    starCell.scaleRange = 0.1;
    starCell.spin = 2*M_PI;;
    starCell.redRange = 0.5;
    starCell.greenRange = 0.5;
    starCell.blueRange = 0.5;
    
    
    emitterLayer.emitterCells = @[cycleCell];
    cycleCell.emitterCells = @[burstCell];
    burstCell.emitterCells = @[starCell];
}

总调用

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // 背景梦幻星空
    do {
        LYBgImageView *animationView = [[LYBgImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
        [self.view addSubview:animationView];
    } while (0);
    
    // 第一条移动星星闪烁动画
    do {
        CGMutablePathRef path = CGPathCreateMutable();
        
        CGPathMoveToPoint(path, NULL, 0, 0);
        CGPathAddCurveToPoint(path, NULL, 50.0, 100.0, 50.0, 120.0, 50.0, 275.0);
        CGPathAddCurveToPoint(path, NULL, 50.0, 275.0, 150.0, 275.0, 160.0, 160.0);
        
        LYMovePathView *animationView = [[LYMovePathView alloc] initWithFrame:CGRectMake(0, 0, 320, 320) movePath:path];
        [self.view addSubview:animationView];
    } while (0);
    
    // 第二条移动星星闪烁动画
    do {
        CGMutablePathRef path = CGPathCreateMutable();
        
        CGPathMoveToPoint(path, NULL, 320 - 0, 320 - 0);
        CGPathAddCurveToPoint(path, NULL, 320 - 50.0, 320 - 100.0, 320 - 50.0, 320 - 120.0, 320 - 50.0, 320 - 275.0);
        CGPathAddCurveToPoint(path, NULL, 320 - 50.0, 320 - 275.0, 320 - 150.0, 320 - 275.0, 160.0, 160.0);
        
        LYMovePathView *animationView = [[LYMovePathView alloc] initWithFrame:CGRectMake(0, 0, 320, 320) movePath:nil];
        [self.view addSubview:animationView];
    } while (0);
    
    // 祝贺花筒,彩色炮竹
    do {
        LYFireworksView *animationView = [[LYFireworksView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
        [self.view addSubview:animationView];
    } while (0);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值