Skip to content

Instantly share code, notes, and snippets.

@YooWaan
Created March 27, 2012 06:28
Show Gist options
  • Save YooWaan/2213301 to your computer and use it in GitHub Desktop.
Save YooWaan/2213301 to your computer and use it in GitHub Desktop.
EmittingCell Sample
//
// DSAppDelegate.m
// DrawSmoke
//
// Created by developer on 12/03/27.
// Copyright (c) 2012 your good mind. All rights reserved.
//
#import "DSAppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#if __has_feature(objc_arc)
#define ARC_ON
#define AUTORELEASE(A) A
#define RETAIN(A) A
#define RELEASE(A)
#else
#define ARC_OFF
#define AUTORELEASE(A) [A autorelease]
#define RETAIN(A) [A retain]
#define RELEASE(A) [A release]
#endif
//
// SmokeView
//
@interface DSSmokeView : UIView
@end
@implementation DSSmokeView
-(id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame]) != nil) {
CAEmitterLayer* fireEmitter = (CAEmitterLayer*)self.layer;
fireEmitter.emitterPosition = CGPointMake(self.bounds.size.width/2, self.bounds.size.height);
fireEmitter.emitterMode = kCAEmitterLayerSurface; //kCAEmitterLayerOutline;
fireEmitter.emitterShape = kCAEmitterLayerLine; // kCAEmitterLayerSphere; kCAEmitterLayerCircle; kCAEmitterLayerPoint;
fireEmitter.emitterSize = CGSizeMake(self.bounds.size.width/5,self.bounds.size.width/5 );
float gas=.6;
CAEmitterCell *smoke = [CAEmitterCell emitterCell];
smoke.BirthRate = 0;//11;
smoke.emissionLongitude = -M_PI ;
smoke.emissionLatitude = -M_PI;
smoke.lifetime = gas*4;
smoke.Velocity = 40;
smoke.VelocityRange = 20;
smoke.emissionRange = M_PI / 4;
smoke.Spin = 1;
smoke.SpinRange = 6;
smoke.yAcceleration = -160;
smoke.Scale = 0.3f;
smoke.AlphaSpeed = -0.22f;
smoke.ScaleSpeed = 0.7f;
smoke.color = [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3*gas] CGColor];
smoke.contents = (id)[[UIImage imageNamed:@"smoke.png"] CGImage];
[smoke setName:@"smoke"];
fireEmitter.emitterCells = [NSArray arrayWithObject:smoke];
}
return self;
}
+ (Class) layerClass {
//configure the UIView to have emitter layer
return [CAEmitterLayer class];
}
-(void)setEmitterPositionFromTouch: (UITouch*)t {
CAEmitterLayer* smokeEmitter = (CAEmitterLayer*)self.layer;
//change the emitter's position
smokeEmitter.emitterPosition = [t locationInView:self];
}
-(void)setIsEmitting:(BOOL)isEmitting {
CAEmitterLayer* smokeEmitter = (CAEmitterLayer*)self.layer;
//turn on/off the emitting of particles
[smokeEmitter setValue:[NSNumber numberWithInt:isEmitting?50:0] forKeyPath:@"emitterCells.smoke.birthRate"];
}
@end
//
// ViewController
//
@interface DSViewController : UIViewController
@end
@implementation DSViewController
-(void) loadView {
CGRect screen = [[UIScreen mainScreen] bounds];
DSSmokeView* smoke = [[DSSmokeView alloc] initWithFrame:screen];
smoke.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
smoke.backgroundColor = [UIColor whiteColor];
self.view = smoke;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[(DSSmokeView*)self.view setEmitterPositionFromTouch: [touches anyObject]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[(DSSmokeView*)self.view setEmitterPositionFromTouch: [touches anyObject]];
[(DSSmokeView*)self.view setIsEmitting:YES];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[(DSSmokeView*)self.view setIsEmitting:NO];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[(DSSmokeView*)self.view setIsEmitting:NO];
}
@end
//
// DSAppDelegate
//
//
// Header File
// #import <UIKit/UIKit.h>
//
// @interface DSAppDelegate : UIResponder <UIApplicationDelegate>
//
// @property (strong, nonatomic) UIWindow *window;
//
// @end
//
//
//
//
@implementation DSAppDelegate
@synthesize window = _window;
#ifdef ARC_OFF
- (void)dealloc {
[_window release];
[super dealloc];
}
#endif
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.rootViewController = AUTORELEASE([[DSViewController alloc] initWithNibName:nil bundle:nil]);
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment