美文网首页
核心动画 - 下雨

核心动画 - 下雨

作者: 君幸食j | 来源:发表于2020-09-20 17:21 被阅读0次

新建一个 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

运行效果如下:

下雨.png

相关文章

  • 核心动画 - 下雨

    新建一个 xcode 项目,然后在 ViewController.m 编写代码实现效果。 ViewControll...

  • IOS 核心动画CoreAniamation总结

    iOS 核心动画是基于CALayer层的动画,UIView动画是系统对核心动画的封装,核心动画相对UIView来说...

  • iOS基础 - 核心动画(转)

    iOS基础 - 核心动画 一、核心动画 l核心动画基本概念 l基本动画 l关键帧动画 l动画组 l转场动画 lCo...

  • iOS核心动画高级技巧 - 8

    iOS核心动画高级技巧 - 1iOS核心动画高级技巧 - 2iOS核心动画高级技巧 - 3iOS核心动画高级技巧 ...

  • iOS动画(Core Animation)

    1.核心动画(Core Animation):强大的动画API 核心动画所在的位置如下图 核心动画位于UIKit的...

  • IOS 常用动画的实现方式整理

    一、CoreAnimation(核心动画) 1.核心动画介绍 1.什么是核心动画 Core Animation可以...

  • UIview动画和核心动画的区别

    UIView和核心动画的区别 核心动画只能添加到CALayer,UIView没有办法使用核心动画 核心动画一切都是...

  • iOS-核心动画

    前言:核心动画的基础知识,包括基本动画、帧动画、转场动画相关知识。 一、核心动画(Core Animation) ...

  • iOS面试个人总结(1)

    动画 1.UIView动画与核心动画的区别? 核心动画只作用在layer. 核心动画修改的值都是假像.它的真实位置...

  • 下雨动画

网友评论

      本文标题:核心动画 - 下雨

      本文链接:https://www.haomeiwen.com/subject/mtrpyktx.html