核心動(dòng)畫 - 下雨

新建一個(gè) xcode 項(xiàng)目,然后在 ViewController.m 編寫代碼實(shí)現(xiàn)效果。

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:@"下大點(diǎn)" 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:@"下小點(diǎn)" 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
    {
        //開(kāi)始下雨
        [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.設(shè)置CAEmitterLayer
    self.emitterLayer = [CAEmitterLayer layer];
    //2.在背景圖上添加粒子圖層
    [self.imageView.layer addSublayer:self.emitterLayer];
    
    //3.發(fā)射形狀--線性
    self.emitterLayer.emitterShape = kCAEmitterLayerLine;
    //發(fā)射模式
    self.emitterLayer.emitterMode = kCAEmitterLayerSurface;
    //發(fā)射源大小
    self.emitterLayer.emitterSize = self.view.frame.size;
    //發(fā)射源位置 y最好不要設(shè)置為0 最好<0
    self.emitterLayer.emitterPosition = CGPointMake(self.view.frame.size.width * 0.5, -10);
    
    //2.配置cell
    CAEmitterCell * emitterCell = [CAEmitterCell emitterCell];
    //粒子內(nèi)容
    emitterCell.contents = (id)[UIImage imageNamed:@"rain_white"].CGImage;
    //每秒產(chǎn)生的粒子數(shù)量的系數(shù)
    emitterCell.birthRate = 25.0f;
    //粒子的生命周期
    emitterCell.lifetime = 20.0f;
    //speed粒子速度.圖層的速率。用于將父時(shí)間縮放為本地時(shí)間,例如,如果速率是2,則本地時(shí)間的進(jìn)度是父時(shí)間的兩倍。默認(rèn)值為1。
    emitterCell.speed = 10.0f;
    //粒子速度系數(shù), 默認(rèn)1.0
    emitterCell.velocity = 10.0f;
    //每個(gè)發(fā)射物體的初始平均范圍,默認(rèn)等于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

運(yùn)行效果如下:

下雨.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容