资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

iOS如何实现大雪纷飞动画-创新互联

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

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网页空间、营销软件、网站建设、柏乡网站维护、网站推广。

1.结果展示

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

iOS如何实现大雪纷飞动画

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];
  }];
}

以上是“iOS如何实现大雪纷飞动画”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:iOS如何实现大雪纷飞动画-创新互联
分享路径:http://cdkjz.cn/article/csgcdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220