zoukankan      html  css  js  c++  java
  • ❄️ 雪花降落

     

    #define SNOWPOSITION (arc4random() % (int)self.view.frame.size.width)

    #define SNOWWIDTH (arc4random() % 20 + 10)

     

     

    @interface XLRightViewController ()

    {

        NSMutableArray * snowArr;

    }

    @end

     

    @implementation XLRightViewController

     

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

    -(void)createUI

    {

        UIImageView * imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];

        imageView.image = [UIImage imageNamed:@"3.jpg"];

        [self.view addSubview:imageView];

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        [self createUI];

        [self createSnowImageView];

    }

    -(void)createSnowImageView

    {

        snowArr = [[NSMutableArray alloc]init];

        //创建20多雪花 循环利用

        for(int i = 0;i < 20;i++)

        {

            float snowWidth = SNOWWIDTH;

            UIImageView * snowImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SNOWPOSITION, -30, snowWidth, snowWidth)];

            snowImageView.image = [UIImage imageNamed:@"snow.png"];

     

            //设置雪花的透明度

            snowImageView.alpha = (arc4random() % 10)/10.0;

        

            [self.view addSubview:snowImageView];

            

            //将雪花视图添加到数组中 循环利用

            [snowArr addObject:snowImageView];

        }

        

        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(makeSnow) userInfo:nil repeats:YES];

    }

    -(void)makeSnow

    {

        //为每朵雪花添加tag

        static int index = 0;

        if(snowArr.count > 0)

        {

            //获取数组中第一朵雪花

            UIImageView * imageView = (UIImageView *)snowArr[0];

            imageView.tag = ++index;

            

            //添加完tag值的这朵雪花 就可以下落了

            [self snowDown:imageView];

            

            //从数组中移除该雪花 等待重用

            [snowArr removeObject:imageView];

        }

    }

    -(void)snowDown:(UIImageView *)imageView

    {

        //为协议中的方法传递参与动画的视图的tag

        [UIView beginAnimations:[NSString stringWithFormat:@"%d",imageView.tag] context:nil];

        

        //设置动画的持续时间

        [UIView setAnimationDuration:6];

        

        //设置当前视图控制器为UIView动画代理

        [UIView setAnimationDelegate:self];

        

        //设置雪花的降落位置及动画停止的方法

        imageView.frame = CGRectMake(imageView.frame.origin.x, self.view.frame.size.height, imageView.frame.size.width, imageView.frame.size.height);

        

        //动画停止的方法--------协议中的方法

        [UIView setAnimationDidStopSelector:@selector(snowStop:)];

        

        //开始动画

        [UIView commitAnimations];

    }

    -(void)snowStop:(NSString *)imageTag

    {

        //获取重复利用的雪花

        UIImageView * imageView = (UIImageView *)[self.view viewWithTag:[imageTag integerValue]];

        

        float width = SNOWWIDTH;

        

        imageView.frame = CGRectMake(SNOWPOSITION, -30, width, width);

        

        //将雪花添加到数组中

        [snowArr addObject:imageView];

    }

     

  • 相关阅读:
    最实用的深度学习教程 Practical Deep Learning For Coders (Kaggle 冠军 Jeremy Howard 亲授)
    线性代数的本质与几何意义 01. 向量是什么?(3blue1brown 咪博士 图文注解版)
    【小蜜蜂老师主讲】基于STM32CubeMX的嵌入式开发基础教程
    【STM32专题教程】STM32CubeMX的安装与快速入门
    【技能大赛真题】2019年物联网国赛任务三题1-无线通信系统
    【CC2530强化实训03】定时器间隔定时实现按键长按与短按
    【CC2530强化实训04】定时器间隔定时实现按键N连击
    【CC2530强化实训02】普通延时函数实现按键的长按与短按
    【CC2530强化实训01】普通延时函数实现按键的长按与短按
    【蓝桥杯单片机12】实时时钟DS1302的基本操作
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6137917.html
Copyright © 2011-2022 走看看