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

    }

     

  • 相关阅读:
    数学基础详解 1——微积分
    logistic回归梯度上升优化算法
    决策树
    西瓜书学习笔记(1)——模型评估与选择
    关于map与set的一点理解;
    STL中的set容器的一点总结(转)
    HDOJ 题目分类
    Train Problem I(栈)
    猜数字(规律)
    Change the ball(找规律)
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6137917.html
Copyright © 2011-2022 走看看