zoukankan      html  css  js  c++  java
  • 封装雪花下落效果

    效果图如下:

    代码如下,新建一个View,使他继承UIView,这里面使用的是LzwSnowView;

    #import "LzwSnowView.h"
    
    @interface LzwSnowView ()
    {
        UIImage *_image;
    }
    @end
    
    @implementation LzwSnowView
    
    
    -(instancetype)initWithFrame:(CGRect)frame{
        
        self = [super initWithFrame:frame];
        
        if (self) {
            //雪花图片
            _image = [UIImage imageNamed:@"Snow"];
            //循环添加图片
            [NSTimer scheduledTimerWithTimeInterval:.8f target:self selector:@selector(addOneSnowflake) userInfo:nil repeats:YES];
        }
        return self;
    }
    
    - (void)addOneSnowflake{
        
        //雪花图片
        UIImageView *imgV = [[UIImageView alloc]initWithImage:_image];
        
        //随机生成雪花位置
        int startX = round(random() % 320);
        int endX = round(random() % 320);
        
        //随机生成雪花的缩放比和速度
        double scale = 1 / round(random() % 100) + 1.0;
        double speed = 1 / round(random() % 100) + 1.0;
    
        //设置雪花的起始位置
        imgV.frame = CGRectMake(startX, -30, 25.f * scale, 25.f * scale);
        imgV.alpha = .5f;
        
        //将雪花添加到view上面
        [self addSubview:imgV];
        [self sendSubviewToBack:imgV];
        
        //动画改变位置
        [UIView animateWithDuration:25 * speed animations:^{
            //曲线
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            imgV.frame = CGRectMake(endX, 1000.f, 25.f * scale, 25.f * scale);
            
            
        } completion:^(BOOL finished) {
            if (finished) {
                [imgV removeFromSuperview];
            }
            
        }];
        
    }
    
    
    @end

    在需要添加雪花的控制器中添加LzwSnowView即可,如下:

    LzwSnowView *snowV = [[LzwSnowView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:snowV];
        [self.view sendSubviewToBack:snowV];

    OK...

  • 相关阅读:
    scanf与scanf_s的区别
    PAT 1041 考试座位号
    PAT1018 锤子剪刀布
    Cookie
    JSP--原理
    多线程练习题
    Java线程--线程的同步与锁
    有关toString()和println(Object)
    Java Web请求和响应机制
    IO流
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/5745953.html
Copyright © 2011-2022 走看看