IOS开发之粒子效果
2015-08-15 12:11
417 查看
粒子效果非常漂亮,在应用中可能用的不多,但是可以给用户眼前一亮的感受。
粒子效果主要用到两个类: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(50, 50);
fireEmitter.emitterSize = CGSizeMake(10, 10);
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(100, 200, 100, 100)];
[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
粒子效果主要用到两个类: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(50, 50);
fireEmitter.emitterSize = CGSizeMake(10, 10);
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(100, 200, 100, 100)];
[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开发系列--地图与定位
- iOS 手势识别 ——长按和轻扫(swipe)
- 解决 iOS8 定位授权的问题
- iOS 手势识别 旋转和缩放(捏合)
- Ios 从无到有项目 MVVM模式(两)
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)
- IOS第九天(3:QQ聊天界面通知的使用)
- IOS第九天(2:QQ聊天界面键盘优化 和自动回复)
- webrtc 编译完成,安装时错误
- IOS第九天(1:QQ聊天界面frame模型)
- webrtc 编译汇编文件时错误
- IOS7 适配以及向下兼容问题
- iOS 手势识别——敲击 Tap
- ios基金会-XCode温馨提示
- iOS概念入门学习-OC-类和对象
- iOS 代理与block 逆向传值 学习
- iOS 代理为啥要用weak修饰? (刨根问底一)
- iOS 万能跳转界面方法(runtime实用篇一)
- iOS 9之Shared Links Extension(Safari Extensibility)