标签:
效果图如下:
代码如下,新建一个View,使他继承UIView,这里面使用的是LzwSnowView;
#import "LzwSnowView.h" @interface LzwSnowView () { UIImage *_image; } @end @implementation LzwSnowView -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { //雪花图片 _image = [UIImage imageNamed:@"Snow"]; //循环添加图片 [NSTimer scheduledTimerWithTimeInterval:.8f target:self selector:@selector(addOneSnowflake) userInfo:nil repeats:YES]; } return self; } - (void)addOneSnowflake{ //雪花图片 UIImageView *imgV = [[UIImageView alloc]initWithImage:_image]; //随机生成雪花位置 int startX = round(random() % 320); int endX = round(random() % 320); //随机生成雪花的缩放比和速度 double scale = 1 / round(random() % 100) + 1.0; double speed = 1 / round(random() % 100) + 1.0; //设置雪花的起始位置 imgV.frame = CGRectMake(startX, -30, 25.f * scale, 25.f * scale); imgV.alpha = .5f; //将雪花添加到view上面 [self addSubview:imgV]; [self sendSubviewToBack:imgV]; //动画改变位置 [UIView animateWithDuration:25 * speed animations:^{ //曲线 [UIView setAnimationCurve:UIViewAnimationCurveLinear]; imgV.frame = CGRectMake(endX, 1000.f, 25.f * scale, 25.f * scale); } completion:^(BOOL finished) { if (finished) { [imgV removeFromSuperview]; } }]; } @end
在需要添加雪花的控制器中添加LzwSnowView即可,如下:
LzwSnowView *snowV = [[LzwSnowView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:snowV];
[self.view sendSubviewToBack:snowV];
OK...
标签:
原文地址:http://www.cnblogs.com/LzwBlog/p/5745953.html