首页 > 代码库 > (5.20)step计数触发定时器--雪花效果
(5.20)step计数触发定时器--雪花效果
// mainViewController.m
// 雪花
//
// Created by pg on 14-5-20.
// Copyright (c) 2014年 mqd. All rights reserved.
//
#import "mainViewController.h"
//时钟刷新步长
static long step;
@interface mainViewController ()
//游戏时钟
@property(strong,nonatomic)CADisplayLink *gameTime;
//雪花图像
@property(strong,nonatomic)UIImage *snowImage;
@end
@implementation mainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//设置背景颜色
[self.view setBackgroundColor:[UIColor blackColor]];
//实例化雪花
self.snowImage = [UIImage imageNamed:@"雪花.png"];
//设置时钟步长
step = 0;
//1,实例化游戏时钟
self.gameTime = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
//2,添加到主运行循环
[self.gameTime addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)step{
step++;
//每秒下十个
if (step % 3 == 0) {
//实例化uiimageview
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.snowImage];
//将uiimage添加到视图
[self.view addSubview:imageView];
//随机位子
CGFloat x = arc4random_uniform(320);
CGFloat y = - self.snowImage.size.height / 2;
[imageView setCenter:CGPointMake(x, y)];
//设置雪花大小
CGFloat r = arc4random_uniform(15) +10;
[imageView setBounds:CGRectMake(0, 0, r, r)];
//实现uiview,动画结束后,删除uiimage
[UIView animateWithDuration:3.0f animations:^{
[imageView setCenter:CGPointMake(arc4random_uniform(320), 450+arc4random_uniform(10)) ];
//设置雪花旋转180度
[imageView setTransform:CGAffineTransformMakeRotation(M_PI)];
//设置雪花透明
[imageView setAlpha:0.3f];
}completion:^(BOOL finished) {
[imageView removeFromSuperview];
}];
}
}
@end