iOS实现大雪纷飞动画
程序员文章站
2023-12-17 14:31:58
本文实例为大家分享了ios实现大雪纷飞动画的具体代码,供大家参考,具体内容如下
1.结果展示
美丽的雪花,勾起了多少美好的回忆。
2.制作思路
其实创作这样一个...
本文实例为大家分享了ios实现大雪纷飞动画的具体代码,供大家参考,具体内容如下
1.结果展示
美丽的雪花,勾起了多少美好的回忆。
2.制作思路
其实创作这样一个大学纷飞的场景是十分简单的,简单到你看了教程之后想不会都不行。ok,下面国际惯例,讲解一下思路吧。
1.创建一个数组用来保存大量的雪花
_imagesarray = [[nsmutablearray alloc] init]; for (int i = 0; i < 1000; ++ i) { uiimageview *imageview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"snow"]]; float x = image_width; imageview.frame = cgrectmake(image_x, -30, x, x); imageview.alpha = image_alpha; [self.view addsubview:imageview]; [_imagesarray addobject:imageview]; }
2.使用时钟(cadisplaylink)来控制下雪,为什么不使用nstimer呢。其实是可以的,只是(cadisplaylink)刷帧更快一些。
//创建时钟,并且添加到主循环中 cadisplaylink *link = [cadisplaylink displaylinkwithtarget:self selector:@selector(makesnow)]; [link addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode];
3.下雪,就是把数组当做队列来使用。
每次从数组头部取出一个雪花并且删除其在数组中的占位。
让雪花飘落,通过uiview动画完成frame,transform等改变。
当动画完成之后,将取出的雪花再次放进数组的尾部
- (void)makesnow { if (_imagesarray.count > 0) { uiimageview *imageview = _imagesarray[0]; [_imagesarray removeobjectatindex:0]; [self snowfall:imageview]; } } - (void)snowfall:(uiimageview *)imageview { [uiview animatewithduration:10 animations:^{ imageview.frame = cgrectmake(imageview.frame.origin.x, main_screen_height, imageview.frame.size.width, imageview.frame.size.height); imageview.transform = cgaffinetransformmakescale(0.3, 0.3); imageview.transform = cgaffinetransformrotate(imageview.transform, m_pi); } completion:^(bool finished) { float x = image_width; imageview.frame = cgrectmake(image_x, -30, x, x); [_imagesarray addobject:imageview]; }]; }
3.有代码有真相
#define image_x arc4random()%(int)main_screen_width #define image_alpha ((float)(arc4random()%10))/10 #define image_width arc4random()%20 + 10 #define plus_height main_screen_height/25 #define main_screen_height [[uiscreen mainscreen] bounds].size.height #define main_screen_width [[uiscreen mainscreen] bounds].size.width #import "viewcontroller.h" @interface viewcontroller () @property (nonatomic ,strong) nsmutablearray *imagesarray; @property (nonatomic , strong) uiimageview *imageview; @end @implementation viewcontroller - (void)loadview { uiimageview *imageview = [[uiimageview alloc]initwithframe:[uiscreen mainscreen].bounds]; imageview.image = [uiimage imagenamed:@"backgound.jpg"]; imageview.contentmode = uiviewcontentmodescaleaspectfill; self.view = imageview; } - (void)viewdidload { [super viewdidload]; _imagesarray = [[nsmutablearray alloc] init]; for (int i = 0; i < 1000; ++ i) { uiimageview *imageview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"snow"]]; float x = image_width; imageview.frame = cgrectmake(image_x, -30, x, x); imageview.alpha = image_alpha; [self.view addsubview:imageview]; [_imagesarray addobject:imageview]; } //创建时钟,并且添加到主循环中 cadisplaylink *link = [cadisplaylink displaylinkwithtarget:self selector:@selector(makesnow)]; [link addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode]; } - (void)makesnow { if (_imagesarray.count > 0) { uiimageview *imageview = _imagesarray[0]; [_imagesarray removeobjectatindex:0]; [self snowfall:imageview]; } } - (void)snowfall:(uiimageview *)imageview { [uiview animatewithduration:10 animations:^{ imageview.frame = cgrectmake(imageview.frame.origin.x, main_screen_height, imageview.frame.size.width, imageview.frame.size.height); imageview.transform = cgaffinetransformmakescale(0.3, 0.3); imageview.transform = cgaffinetransformrotate(imageview.transform, m_pi); } completion:^(bool finished) { float x = image_width; imageview.frame = cgrectmake(image_x, -30, x, x); [_imagesarray addobject:imageview]; }]; }
4.demo也不能少
下载地址:snow
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。