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

        }];

        

        

    }

  • 相关阅读:
    Linux内核设计与实现 总结笔记(第五章)系统调用
    Linux内核设计与实现 总结笔记(第四章)进程调度
    Linux内核设计与实现 总结笔记(第三章)进程
    Linux内核设计与实现 总结笔记(第二章)
    4412 移植x264并且YUV422转x264
    4412 使用usb摄像头拍照YUYV格式
    LDD3 第15章 内存映射和DMA
    LDD3 第13章 USB驱动程序
    ldd3 第12章 PCI驱动程序
    4412 移植mpu9250尝试
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6246172.html
Copyright © 2011-2022 走看看