Create a gist now

Instantly share code, notes, and snippets.

Ultra chicken CAEmitterLayer particle system
//
// Created by tdimson on 9/5/12.
#import <QuartzCore/QuartzCore.h>
#import "SFSChickenScreen.h"
@implementation SFSChickenScreen {
__weak CAEmitterLayer *_chickenEmitter;
CGFloat _decayAmount;
}
- (id) initWithFrame:(CGRect)frame chickenPosition:(CGPoint)position {
if((self = [super initWithFrame:frame])) {
self.userInteractionEnabled = NO;
self.backgroundColor = [UIColor clearColor];
_chickenEmitter = (CAEmitterLayer*)self.layer;
_chickenEmitter.emitterPosition = position;
_chickenEmitter.emitterSize = self.bounds.size;
_chickenEmitter.emitterShape = kCAEmitterLayerPoint;
CAEmitterCell *chicken = [CAEmitterCell emitterCell];
chicken.contents = (__bridge id)[[UIImage imageNamed:@"ArgChicken.png"] CGImage];
chicken.name = @"chicken";
chicken.birthRate = 5;
chicken.lifetime = 5.0;
chicken.color = [[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0] CGColor];
chicken.alphaSpeed = 10.0;
chicken.velocity = 200;
chicken.velocityRange = 50;
chicken.emissionRange = (CGFloat) M_PI_2;
chicken.emissionLongitude = (CGFloat)(M_PI_2 + M_PI_4);
chicken.scale = 0.9;
chicken.scaleRange = 0.2;
chicken.spin = 0;
chicken.spinRange = 10;
chicken.yAcceleration = 250;
_chickenEmitter.emitterCells = [NSArray arrayWithObject:chicken];
}
return self;
}
- (void)stopEmitting {
_chickenEmitter.birthRate = 0.0;
}
static NSTimeInterval const kDecayInterval = 0.1;
- (void) decayStep {
_chickenEmitter.birthRate -=_decayAmount;
if (_chickenEmitter.birthRate < 0) {
_chickenEmitter.birthRate = 0;
} else {
[self performSelector:@selector(decayStep) withObject:nil afterDelay:kDecayInterval];
}
}
- (void) decayOverTime:(NSTimeInterval)interval {
_decayAmount = (CGFloat) (_chickenEmitter.birthRate / (interval / kDecayInterval));
[self decayStep];
}
+ (Class) layerClass {
return [CAEmitterLayer class];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment