标签:
- (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];
}
@interface CAEmitterLayerView : UIView
/**
* 模仿setter.getter方法
*/
- (void)setEmitterLayer:(CAEmitterLayer *)layer;
- (CAEmitterLayer *)emitterLayer;
/**
* 显示出当前view
*/
- (void)show;
/**
* 隐藏当前view
*/
- (void)hide;
@end
二、封装 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
@interface SnowView :CAEmitterLayerView
@end
封装 CAEmitterLayer#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
#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
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/baitxaps/article/details/46869389