1.首先设置一下落雪的背景
//创建落雪
UIImageView *bgView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
bgView.image = [UIImage imageNamed:@"snowbg.jpg"];
//设置图片填充模式
bgView.contentMode = UIViewContentModeScaleAspectFill;
//添加背景视图
[self.view addSubview: bgView];
//开始落雪
[self startLink];
2.实现落雪花得方法和停止落雪的方法
//开始定时器
- (void) startLink
{
//创建定时器
_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(startSnow)];
//添加到运行循环
[_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//改变循环次数每秒执行60/4
_link.frameInterval = 4;
}
//停止定时
- (void) stopLink
{
[_link invalidate];
_link = nil;
}
3.创建雪花
- (void) startSnow
{
//创建雪花视图
UIImageView *snow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"雪花.png"]];
//设置雪花中心点
CGSize screenSize = [UIScreen mainScreen].bounds.size;
//设置雪花中心点
CGFloat x = arc4random_uniform(screenSize.width);
CGFloat y = -snow.bounds.size.height*0.5;
//设置雪花的中心点
snow.center = CGPointMake(x, y);
//随机获得雪花缩放的比例
CGFloat scale = arc4random_uniform(31)/100.0+0.1;
snow.transform = CGAffineTransformMakeScale(scale, scale);
[self.view addSubview:snow];
//添加动画降落
[UIView animateWithDuration:10.0 animations:^{
//旋转动画
snow.transform = CGAffineTransformRotate(snow.transform, M_PI);
//随机设置底部的坐标
CGFloat x1 = arc4random_uniform(screenSize.width);
CGFloat y1 = CGRectGetHeight(self.view.frame)+snow.bounds.size.height;
snow.center = CGPointMake(x1, y1);
} completion:^(BOOL finished) {
//清除下落之后的雪花
[snow removeFromSuperview];
}];
}
代码已经打包到资源下载了(包含图片)。有兴趣的朋友可以下载来看看。