IOS开发之粒子效果

粒子效果非常漂亮,在应用中可能用的不多,但是可以给用户眼前一亮的感受。

粒子效果主要用到两个类:CAEmitterLayer和CAEmitterCell.

步骤如下:

1.添加框架

#import <QuartzCore/QuartzCore.h>

2.设定两个类

CAEmitterLayer和CAEmitterCell

首先定义一个View

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        fireEmitter = (CAEmitterLayer *)self.layer;

        fireEmitter.emitterPosition = CGPointMake(5050);

        fireEmitter.emitterSize = CGSizeMake(1010);

        fireEmitter.renderMode = kCAEmitterLayerAdditive;

        CAEmitterCell *fire = [CAEmitterCell  emitterCell];

        fire.birthRate = 0;

        fire.lifetime = 3.0;

        fire.lifetimeRange = 0.5;

        fire.color = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.1].CGColor;

        fire.contents = (id)[[UIImage imageNamed:@"Particles_fire"]CGImage];

        fire.velocity = 10;

        fire.velocityRange = 20;

        fire.emissionRange = M_PI_2;

        fire.scaleSpeed = 0.3;

        fire.spin = 0.5;

        

        [fire setName:@"fire"];

        

        //add the cell to the layer and we're done

        fireEmitter.emitterCells = [NSArray arrayWithObject:fire];

    }

    return self;

}

- (void)setEmitterPositionFromTouch:(UITouch *)t

{

    fireEmitter.emitterPosition = [t locationInView:self];

}


- (void)setIsEmitting:(BOOL)isEmitting

{

    [fireEmitter setValue:[NSNumber numberWithInt:isEmitting?200:0]forKeyPath:@"emitterCells.fire.birthRate"];

}


+ (Class)layerClass

{

    return [CAEmitterLayer class];

}


3.在ViewController中控制它

    particle = [[ZXCParticleVeiw alloc]initWithFrame:CGRectMake(100200100100)];

    [self.view addSubview:particle];


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    [particle setEmitterPositionFromTouch:[touches anyObject]];

    [particle setIsEmitting:YES];

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    [particle setIsEmitting:NO];

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    [particle setIsEmitting:NO];

}



转载地址:http://www.cnblogs.com/andyque/archive/2012/01/10/2317968.html

你可能感兴趣的:(ios,粒子,效果)