iOS如何实现大雪纷飞动画

  介绍

小编给大家分享一下iOS如何实现大雪纷飞动画,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!

<强> 1。结果展示

美丽的雪花,勾起了多少美好的回忆。

 iOS如何实现大雪纷飞动画

<强> 2。制作思路

其实创作这样一个大学纷飞的场景是十分简单的,简单到你看了教程之后想不会都不行对吧、下面国际惯例,讲解一下思路吧。

1。创建一个数组用来保存大量的雪花

_imagesArray =, [[NSMutableArray  alloc], init);   for 才能;(int 小姐:=,0;,小姐:& lt;, 1000;, + +,我),{   ,,,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 (makeSnow)];   [link  addToRunLoop: [NSRunLoop  mainRunLoop], forMode: NSDefaultRunLoopMode];

3。下的雪,就是把数组当做队列来使用。

每次从数组头部取出一个雪花并且删除其在数组中的占位。
让雪花飘落,通过UIView动画完成帧,变换等改变。
当动画完成之后,将取出的雪花再次放进数组的尾部

安康;makeSnow (void)   {   if 才能;(_imagesArray.count 祝辞,0),{   ,,,UIImageView  * imageView =, _imagesArray [0];   ,,,(_imagesArray  removeObjectAtIndex: 0);   ,,,(self 降雪:imageView);   ,,}   }      安康;(空白)降雪:(UIImageView  *) imageView   {   (才能UIView  animateWithDuration: 10,动画:^ {   ,,,imageView.frame =, CGRectMake (Main_Screen_Height, imageView.frame.origin.x,还以为,imageView.frame.size.width, imageView.frame.size.height);   ,,,imageView.transform =, CGAffineTransformMakeScale (0.3, 0.3);   ,,,imageView.transform =, CGAffineTransformRotate (M_PI imageView.transform也);   },才能完成:^ (BOOL 完成),{   ,,,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 ,,,,,((浮动)(arc4random () % 10))/10   # define  IMAGE_WIDTH ,,,,, arc4random () % 20, + 10   # define  PLUS_HEIGHT ,,,,, Main_Screen_Height/25      # define  Main_Screen_Height ,, [[UIScreen  mainScreen],边界].size.height   # define  Main_Screen_Width ,,, [[UIScreen  mainScreen],边界].size.width      # import “ViewController.h"      @interface  ViewController  ()   @property  (nonatomic 强),NSMutableArray  * imagesArray;   @property  (nonatomic ,,强烈),UIImageView  * imageView;   @end      @implementation  ViewController      loadView安康;(void)   {   UIImageView 才能;* imageView =, [[UIImageView  alloc] initWithFrame: [UIScreen  mainScreen] .bounds];   imageView.image 才能=,(UIImage  imageNamed: @" backgound.jpg");   时间=imageView.contentMode 才能;UIViewContentModeScaleAspectFill;   时间=self.view 才能;imageView;      }      安康;viewDidLoad (void)   {   (才能super  viewDidLoad);      _imagesArray 才能=,[[NSMutableArray  alloc], init);   for 才能;(int 小姐:=,0;,小姐:& lt;, 1000;, + +,我),{   ,,,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 (makeSnow)];   (才能link  addToRunLoop: [NSRunLoop  mainRunLoop], forMode: NSDefaultRunLoopMode);   }         安康;makeSnow (void)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

iOS如何实现大雪纷飞动画