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

     

    //CADisplayLink  和屏幕刷新频率相同

     

    //开始下雪

    - (void) beginShow{

        

        //启动定时器,使得一直调用setNeedsDisplay从而调用- (void) drawRect:(CGRect )rect

        //不得手动调用- (void) drawRect:(CGRect )rect

        CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];

        //让定时器循环调用

        [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

    }

     

    - (void) drawRect:(CGRect)rect {

        

        //控制雪花最多的个数

        //    if (self.subviews.count >250) {

        //        return;

        //    }

        if (self.subviews.count >20) {

            return;

        }

        //雪花的宽度

        int width = arc4random() % 5;

        while (width < 2) {

            width = arc4random() % 5;

        }

        //雪花的速度

        int speed = arc4random() % 10;

        while (speed < 5) {

            speed = arc4random() % 10;

        }

        

        //雪花起点y

        int startY = - (arc4random() % 100);

        //雪花起点x

        int startX = arc4random() % (int) [UIScreen mainScreen].bounds.size.width;

        //雪花终点x

        int endX = arc4random() % (int) [UIScreen mainScreen].bounds.size.width;

        //int endX = arc4random() % (int)_bgView.height;

        //int endX = arc4random() % (int)(_bgView.height - 71*GOP_HeightR);//

        

        

        

        

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.snowImgName]];

        imageView.frame = CGRectMake(startX, startY, width, width);

        [self addSubview:imageView];

        

        

        //设置动画

        [UIView animateWithDuration:speed animations:^{

            //设置雪花最终的frame

            //        imageView.frame = CGRectMake(endX, [UIScreen mainScreen].bounds.size.height, width, width);

            //雪花最终下落的高度为_bgView的高度减去71*GOP_HeightR的高度

            imageView.frame = CGRectMake(endX, _bgView.height-71*GOP_HeightR, width, width);

            

            //设置雪花的旋转

            imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI);

            //设置雪花透明度,使得雪花快落地的时候就像快消失的一样

            //imageView.alpha = 0.3;

        } completion:^(BOOL finished) {

            [imageView removeFromSuperview];

        }];

        

        

    }

  • 相关阅读:
    Android的LinearLayout中orientation默认值为什么是HORIZONTAL
    Android中HttpURLConnection对象是怎么生成的
    记一个擦除硬盘数据,防止已删除文件被恢复的程序
    添加一个Android框架层的系统服务与实现服务的回调
    在 Activity 中实现 getContentView 操作
    (01)明明配置了log4j.properties为什么还是不打印日志
    (05)pom.xml文件报错web.xml is missing and <failOnMissingWebXml> is set to true
    (04)maven中的几个常用插件
    (03)开发环境eclipse、myEclipse本地tomcat调试发布maven项目遇到的糟心事
    (04)Storm与Kafka结合使用简单案例
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6246172.html
Copyright © 2011-2022 走看看