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];

    }

     

  • 相关阅读:
    【WPF】操作RichTextBox(取值、赋值、清空、滚动条自动滚动实例、文本自动滚动实例)
    系统初始化 服务列表
    多个filter如何决定调用顺序
    IE浏览器 查看Form对象
    java try_catch 分析
    关于ClassLoader 和Class的俩个记录
    lis分析之一一批处理(任务)如何连接数据库的
    document.all("div).style.display = "none"与 等于""的区别
    Mybatis Util包
    Spring创建bean对象的三种方式
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6137917.html
Copyright © 2011-2022 走看看