新建一个 xcode 项目,然后在 ViewController.m 编写代码实现效果。
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic,strong)CAEmitterLayer * emitterLayer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUpUI];
[self setUpEmitter];
}
-(void)setUpUI
{
self.imageView = [[UIImageView alloc] init];
self.imageView.frame = self.view.frame;
self.imageView.image = [UIImage imageNamed:@"rain"];
[self.view addSubview:self.imageView];
//下雨按钮
UIButton * startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:startBtn];
startBtn.frame = CGRectMake(20, self.view.frame.size.height-60, 80, 40);
startBtn.backgroundColor = [UIColor whiteColor];
[startBtn setTitle:@"雨停了" forState:UIControlStateNormal];
[startBtn setTitle:@"下雨" forState:UIControlStateSelected];
[startBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[startBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[startBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//雨量按钮
UIButton * rainBigBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:rainBigBtn];
rainBigBtn.tag = 100;
rainBigBtn.frame = CGRectMake(140, self.view.frame.size.height-60, 80, 40);
rainBigBtn.backgroundColor = [UIColor whiteColor];
[rainBigBtn setTitle:@"下大点" forState:UIControlStateNormal];
[rainBigBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[rainBigBtn addTarget:self action:@selector(rainBigClick:) forControlEvents:UIControlEventTouchUpInside];
//雨量按钮
UIButton * rainSmallBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:rainSmallBtn];
rainSmallBtn.tag = 200;
rainSmallBtn.frame = CGRectMake(240, self.view.frame.size.height-60, 80, 40);
rainSmallBtn.backgroundColor = [UIColor whiteColor];
[rainSmallBtn setTitle:@"下小点" forState:UIControlStateNormal];
[rainSmallBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[rainSmallBtn addTarget:self action:@selector(rainSmallClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)buttonClick:(UIButton *)btn
{
btn.selected = !btn.selected;
if (btn.selected)
{
//停止下雨
[self.emitterLayer setValue:@0.0f forKey:@"birthRate"];
}
else
{
//开始下雨
[self.emitterLayer setValue:@1.0f forKey:@"birthRate"];
}
}
-(void)rainBigClick:(UIButton *)btn
{
NSInteger rate = 1;
CGFloat scale = 0.05;
if (self.emitterLayer.birthRate < 30)
{
[self.emitterLayer setValue:@(self.emitterLayer.birthRate + rate) forKey:@"birthRate"];
[self.emitterLayer setValue:@(self.emitterLayer.scale + scale) forKey:@"scale"];
}
}
-(void)rainSmallClick:(UIButton *)btn
{
NSInteger rate = 1;
CGFloat scale = 0.05;
if (self.emitterLayer.birthRate > 1)
{
[self.emitterLayer setValue:@(self.emitterLayer.birthRate - rate) forKey:@"birthRate"];
[self.emitterLayer setValue:@(self.emitterLayer.scale - scale) forKey:@"scale"];
}
}
-(void)setUpEmitter
{
//1.设置CAEmitterLayer
self.emitterLayer = [CAEmitterLayer layer];
//2.在背景图上添加粒子图层
[self.imageView.layer addSublayer:self.emitterLayer];
//3.发射形状--线性
self.emitterLayer.emitterShape = kCAEmitterLayerLine;
//发射模式
self.emitterLayer.emitterMode = kCAEmitterLayerSurface;
//发射源大小
self.emitterLayer.emitterSize = self.view.frame.size;
//发射源位置 y最好不要设置为0 最好<0
self.emitterLayer.emitterPosition = CGPointMake(self.view.frame.size.width * 0.5, -10);
//2.配置cell
CAEmitterCell * emitterCell = [CAEmitterCell emitterCell];
//粒子内容
emitterCell.contents = (id)[UIImage imageNamed:@"rain_white"].CGImage;
//每秒产生的粒子数量的系数
emitterCell.birthRate = 25.0f;
//粒子的生命周期
emitterCell.lifetime = 20.0f;
//speed粒子速度.图层的速率。用于将父时间缩放为本地时间,例如,如果速率是2,则本地时间的进度是父时间的两倍。默认值为1。
emitterCell.speed = 10.0f;
//粒子速度系数, 默认1.0
emitterCell.velocity = 10.0f;
//每个发射物体的初始平均范围,默认等于0
emitterCell.velocityRange = 10.0f;
//粒子在y方向的加速的
emitterCell.yAcceleration = 1000.0f;
//粒子缩放比例: scale
emitterCell.scale = 0.1f;
//粒子缩放比例范围:scaleRange
emitterCell.scaleRange = 0.0f;
//3.添加到图层上
self.emitterLayer.emitterCells = @[emitterCell];
}
@end
运行效果如下: