使用带粒子效果的 CAEmitterLayer
1.用CAEmitterLayer产生粒子效果
2.封装CAEmitterLayer
3.封装下雪、下雨的粒子效果控件
一、用CAEmitterLayer产生粒子效果
- (void)emitterLayer {
// 1.创建出Layer
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
// 显示边框
emitterLayer.borderWidth = 1.f;
// 给定尺寸
emitterLayer.frame = CGRectMake(100, 100, 100, 100);
//emitterLayer.mask = YES;不超过范围
// 发射点
emitterLayer.emitterPosition = CGPointMake(0,0);
// 发射模式
emitterLayer.emitterMode =kCAEmitterLayerSurface;
// 发射形状
emitterLayer.emitterShape =kCAEmitterLayerLine;
// 加入layer
[self.view.layeraddSublayer:emitterLayer];
// 2.创建粒子
CAEmitterCell *cell = [CAEmitterCell emitterCell];
// 粒子产生率
cell.birthRate = 10.f;
// 粒子生命周期
cell.lifetime = 10.f;
// 速度值
cell.velocity = 10;
// 速度值的微调值
cell.velocityRange = 3.f;
// y轴加速度
cell.yAcceleration = 2.f;
// 发射角度
cell.emissionRange = 4.f * M_1_PI;
// 设置粒子颜色
cell.color = [UIColorblackColor].CGColor;
// 设置图片
cell.contents = (__bridgeid)([UIImage imageNamed:@"snow"].CGImage);
// 让CAEmitterCell与CAEmitterLayer产生关联
emitterLayer.emitterCells = @[cell];
}
二、封装CAEmitterLayer
@interface CAEmitterLayerView : UIView
/**
* 模仿setter.getter方法
*/
- (void)setEmitterLayer:(CAEmitterLayer *)layer;
- (CAEmitterLayer *)emitterLayer;
/**
* 显示出当前view
*/
- (void)show;
/**
* 隐藏当前view
*/
- (void)hide;
@end
二、封装 CAEmitterLayer
1.替换CAEmitterLayer成UIView子类的backedLayer
2.将CAEmitterLayer封装的类作为“抽象”父类
#import "CAEmitterLayerView.h"
@interface CAEmitterLayerView () {
//因为 CAEmitterLayer的參数许多,採用这样的小技巧,设置set,get方法
CAEmitterLayer *_emitterLayer;
}
@end
@implementation CAEmitterLayerView
+ (Class)layerClass {//必需要写这个,否则新建返回layer,不是 CAEmitterLayer
return [CAEmitterLayerclass];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [superinitWithFrame:frame];
if (self) {
_emitterLayer = (CAEmitterLayer *)self.layer;
}
return self;
}
- (void)show {
}
- (void)hide {
}
- (void)setEmitterLayer:(CAEmitterLayer *)layer {
_emitterLayer = layer;
}
- (CAEmitterLayer *)emitterLayer {
return_emitterLayer;
}
@end
- (void)viewDidLoad {
[superviewDidLoad];
CAEmitterLayerView *layerView = [[CAEmitterLayerView alloc]initWithFrame:CGRectMake(100,100,100,100)];
NSLog(@"%@", layerView.layer);
}
三、封装下雪、下雨的粒子效果控件
#import "CAEmitterLayerView.h"
@interface RainView :CAEmitterLayerView
@end
#import "RainView.h"
@implementation RainView
- (instancetype)initWithFrame:(CGRect)frame {
self = [superinitWithFrame:frame];
if (self) {
//初始化设置
[selfsetup];
}
return self;
}
- (void)setup {
self.emitterLayer.masksToBounds =YES;
self.emitterLayer.emitterShape =kCAEmitterLayerLine;
self.emitterLayer.emitterMode =kCAEmitterLayerSurface;
self.emitterLayer.emitterSize =self.frame.size;
self.emitterLayer.emitterPosition =CGPointMake(self.bounds.size.width /2.f, - 20);
}
- (void)show {
// 配置
CAEmitterCell *rainflake = [CAEmitterCell emitterCell];
rainflake.birthRate =25.f;
rainflake.speed =10.f;
rainflake.velocity =10.f;
rainflake.velocityRange =10.f;
rainflake.yAcceleration =1000.f;
rainflake.contents = (__bridgeid)([UIImage imageNamed:@"rain"].CGImage);
rainflake.color = [UIColorblackColor].CGColor;
rainflake.lifetime =7.f;
rainflake.scale =0.2f;
rainflake.scaleRange =0.f;
// 加入动画
self.emitterLayer.emitterCells =@[rainflake];
}
@end
封装 CAEmitterLayer
@interface SnowView :CAEmitterLayerView
@end
#import "SnowView.h"
@implementation SnowView
- (instancetype)initWithFrame:(CGRect)frame {
self = [superinitWithFrame:frame];
if (self) {
//初始化设置
[selfsetup];
}
return self;
}
- (void)setup {
self.emitterLayer.masksToBounds =YES;
self.emitterLayer.emitterShape =kCAEmitterLayerLine;
self.emitterLayer.emitterMode =kCAEmitterLayerSurface;
self.emitterLayer.emitterSize =self.frame.size;
self.emitterLayer.emitterPosition =CGPointMake(self.bounds.size.width /2.f, - 20);
}
- (void)show {
// 配置
CAEmitterCell *snowflake = [CAEmitterCell emitterCell];
snowflake.birthRate =1.f;
snowflake.speed =10.f;
snowflake.velocity =2.f;
snowflake.velocityRange =10.f;
snowflake.yAcceleration =10.f;
snowflake.emissionRange =0.5 *M_PI;
snowflake.spinRange =0.25 *M_PI;
snowflake.contents = (__bridgeid)([UIImage imageNamed:@"snow"].CGImage);
snowflake.color = [UIColorredColor].CGColor;
snowflake.lifetime =60.f;
snowflake.scale =0.5;
snowflake.scaleRange =0.3;
// 加入动画
self.emitterLayer.emitterCells =@[snowflake];
}
@end
封装 CAEmitterLayer
#import "CAEmitterLayerView.h"
#import "SnowView.h"
#import "RainView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
UIImageView *alphaView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
alphaView1.image = [UIImage imageNamed:@"alpha"];
UIImageView *alphaView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
alphaView2.image = [UIImage imageNamed:@"alpha"];
// 加入下雪效果
CAEmitterLayerView *snowView = [[SnowView alloc]initWithFrame:CGRectMake(100,100,100,100)];
snowView.maskView = alphaView1;//加maskView。效果不突兀
[self.viewaddSubview:snowView];
[snowViewshow];
// 加入下雨效果
CAEmitterLayerView *rainView = [[RainView alloc]initWithFrame:CGRectMake(100,210,100,100)];
rainView.maskView = alphaView2;
[self.viewaddSubview:rainView];
[rainViewshow];
}
@end