美文网首页
iOS开发,使用CADisplayLink实现简单的“下雪花”动

iOS开发,使用CADisplayLink实现简单的“下雪花”动

作者: aiq西米 | 来源:发表于2017-11-22 10:25 被阅读150次

    前言

    CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器。我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个runloop中,并给它提供一个 target 和selector 在屏幕刷新的时候调用。参考CADisplayLink

    下雪花例子

    snowDemo.gif

    使用CADisplayLink就如使用NSTimer一样简单。下面直接上“下雪花”的Demo。

    第一步:找一张白色的雪花图片

    第二步:XCode新建一个工程(或在原有工程),把雪花图片拖到工程里,添加动画引擎库。

    添加动画引擎库.png

    第三步:在控制器中 #import <QuartzCore/QuartzCore.h>

    第四步:编码实现,具体请看代码注释。

    #import "ViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    #define SCREEN_SIZE [UIScreen mainScreen].bounds.size
    
    static long steps;
    
    @interface ViewController ()
    
    /// 游戏时钟
    @property (strong, nonatomic) CADisplayLink *gameTimer;
    /// 雪花
    @property (strong, nonatomic) UIImage *snowImage;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor blackColor];
        
        steps = 0;
        
        // 初始化雪花
        self.snowImage = [UIImage imageNamed:@"雪花.png"];
        
        // 创建游戏时钟
        self.gameTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
        
        // 将时钟添加到主循环
        [self.gameTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    }
    
    #pragma mark - 时钟触发方法
    // 在box2d物理引擎中,时钟触发的方法名就是step
    // 默认每秒刷新60次
    - (void)step
    {
        steps ++;
        
        if (steps % 10 == 0) { // 控制每秒出现6个雪花,(即60%10)
            
            NSLog(@"step ");
            
            // 创建雪花view
            UIImageView *snowView = [[UIImageView alloc]initWithImage:self.snowImage];
            [self.view addSubview:snowView];
            
            // 设置雪花随机大小 :10~20
            CGFloat r = arc4random_uniform(10) + 10.0;
            snowView.frame = CGRectMake(0, 0, r, r);
            
            // 将雪花添加到屏幕上面 随机位置
            CGFloat x = arc4random_uniform(SCREEN_SIZE.width);
            CGFloat y = -r;
            snowView.center = CGPointMake(x, y);
            
            // 创建动画,移动雪花,动画结束后删除
            [UIView animateWithDuration:6.0 animations:^{
                
                // 改变雪花位置
                snowView.center = CGPointMake(arc4random_uniform(SCREEN_SIZE.width), arc4random_uniform(100)+ SCREEN_SIZE.height);
                
                // 改变雪花透明度
                snowView.alpha = 0.2;
                
                // 旋转180度
                [snowView setTransform:CGAffineTransformMakeRotation(M_PI)];
                
            } completion:^(BOOL finished) {
                // 千万要记得在动画结束后删除掉!
                [snowView removeFromSuperview];
            }];
        }
    }
    
    /// 隐藏状态栏
    - (BOOL)prefersStatusBarHidden
    {
        return YES;
    }
    
    /// 停止动画时钟(将其从主循环中移除)
    - (void)stopGameTimer
    {
        [self.gameTimer invalidate];
    }
    @end
    
    

    结束

    CADisplayLink在游戏开发中也比较重要,可以开发比如微信打飞机等游戏。

    相关文章

      网友评论

          本文标题:iOS开发,使用CADisplayLink实现简单的“下雪花”动

          本文链接:https://www.haomeiwen.com/subject/ugcnvxtx.html