iOS 粒子系统

来源:互联网 发布:2017淘宝生意差 编辑:程序博客网 时间:2024/05/16 12:51

-(void)awakeFromNib

{

    

    fireEmitter = (CAEmitterLayer*)self.layer;//设置自己的层为CAEmitterLayer的层

    

    fireEmitter.emitterPosition =CGPointMake(50,50); //设置发射器位置

    fireEmitter.emitterSize = CGSizeMake(10, 10); //发射器尺寸

    

    CAEmitterCell* fire = [CAEmitterCellemitterCell];//发射器单元

    fire.birthRate =0;//产生速度

    fire.lifetime =3.0;//生命

    fire.lifetimeRange =0.5;//生命范围

    fire.color = [[UIColorcolorWithRed:0.8green:0.4blue:0.2alpha:0.1]CGColor]; //颜色 注意一定是cgcolor

    fire.contents = (id)[[UIImageimageNamed:@"Particles_fire.png"]CGImage];//图片 注意一定是cgimage

    [fire setName:@"fire"];//name


    fire.velocity =10;//速度

    fire.velocityRange =20;//速度范围

    fire.emissionRange =M_PI_2;//散发

    

    fire.scaleSpeed =0.3;//速度放大

    fire.spin =0.5;//旋转,眩晕

    

    fireEmitter.renderMode =kCAEmitterLayerAdditive;//叠加模式

    

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

    fireEmitter.emitterCells = [NSArrayarrayWithObject:fire];//单元数组

    

}


+ (Class) layerClass//3

{

    //configure the UIView to have emitter layer

    return [CAEmitterLayerclass];  //返回层类

}


-(void)setEmitterPositionFromTouch: (UITouch*)t

{

    //change the emitter's position

    fireEmitter.emitterPosition = [tlocationInView:self];

}


-(void)setIsEmitting:(BOOL)isEmitting

{

    //turn on/off the emitting of particles

    [fireEmittersetValue:[NSNumbernumberWithInt:isEmitting?200:0]forKeyPath:@"emitterCells.fire.birthRate"];

}


//viewController

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

    [fireViewsetEmitterPositionFromTouch: [touchesanyObject]];

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [fireViewsetEmitterPositionFromTouch: [touchesanyObject]];

    [fireViewsetIsEmitting:YES];

}


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

    [fireViewsetIsEmitting:NO];

}


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

    [fireViewsetIsEmitting:NO];

}


原创粉丝点击