iOS实现大雪纷飞动画

 更新时间:2018年06月22日 10:07:02   作者:桂雏菊  
这篇文章主要为大家详细介绍了iOS实现大雪纷飞动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 99% iOS开发都不知道的KVO崩溃分析详解

    99% iOS开发都不知道的KVO崩溃分析详解

    这篇文章主要为大家介绍了99% iOS开发都不知道的KVO崩溃分析详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • iOS新增绘制圆的方法实例代码

    iOS新增绘制圆的方法实例代码

    这篇文章主要给大家介绍了关于iOS新增绘制圆的方法,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • 详解iOS App开发中改变UIButton内部控件的基本方法

    详解iOS App开发中改变UIButton内部控件的基本方法

    这篇文章主要介绍了iOS App开发中改变UIButton内部控件的基本方法,文章开头也顺带总结了一些UIButton的基本用法,示例代码为Objective-C,需要的朋友可以参考下
    2016-03-03
  • iOS实现顶部标签式导航栏及下拉分类菜单

    iOS实现顶部标签式导航栏及下拉分类菜单

    这篇文章主要为大家详细介绍了iOS实现顶部标签式导航栏及下拉分类菜单的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • iOS仿微信摇一摇动画效果加震动音效实例

    iOS仿微信摇一摇动画效果加震动音效实例

    这篇文章主要介绍了iOS仿微信摇一摇动画效果加震动音效实例,详细介绍了微信摇一摇功能的实现原理,非常具有实用价值,需要的朋友可以参考下。
    2017-03-03
  • iOS如何为圆角添加阴影效果示例代码

    iOS如何为圆角添加阴影效果示例代码

    最近一个项目中需要用到投影的效果,还要是圆角,通过查找相关的资料终于解决了,所以觉着有必要分享出来,下面这篇文章主要给大家介绍了关于iOS如何为圆角添加阴影效果的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10
  • Swift 进阶 —— map 和 flatMap的使用

    Swift 进阶 —— map 和 flatMap的使用

    这篇文章主要介绍了Swift map和flatMap的相关资料,帮助大家更好的理解和使用Swift,感兴趣的朋友可以了解下
    2020-09-09
  • IOS 禁止缩放页面的实现方法

    IOS 禁止缩放页面的实现方法

    这篇文章主要介绍了IOS 禁止缩放页面的实现方法的相关资料,这里主要介绍了IOS 10如何通过设置来实现禁止缩放及实现方法,需要的朋友可以参考下
    2017-07-07
  • iOS如何将图片裁剪成圆形

    iOS如何将图片裁剪成圆形

    这篇文章主要为大家详细介绍了iOS如何将图片裁剪成圆形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS性能优化浅析

    iOS性能优化浅析

    给大家通过理论和经验分析了IOS性能优化各方面的问题,以及处理的对应办法,有需要的朋友参考下。
    2018-02-02

最新评论