iOS如何實(shí)現(xiàn)大雪紛飛動(dòng)畫

小編給大家分享一下iOS如何實(shí)現(xiàn)大雪紛飛動(dòng)畫,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了峨邊彝族免費(fèi)建站歡迎大家使用!

1.結(jié)果展示

美麗的雪花,勾起了多少美好的回憶。

iOS如何實(shí)現(xiàn)大雪紛飛動(dòng)畫

2.制作思路

其實(shí)創(chuàng)作這樣一個(gè)大學(xué)紛飛的場景是十分簡單的,簡單到你看了教程之后想不會(huì)都不行。OK,下面國際慣例,講解一下思路吧。

1.創(chuàng)建一個(gè)數(shù)組用來保存大量的雪花

_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.使用時(shí)鐘(CADisplayLink)來控制下雪,為什么不使用NSTimer呢。其實(shí)是可以的,只是(CADisplayLink)刷幀更快一些。

//創(chuàng)建時(shí)鐘,并且添加到主循環(huán)中
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(makeSnow)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

3.下雪,就是把數(shù)組當(dāng)做隊(duì)列來使用。

每次從數(shù)組頭部取出一個(gè)雪花并且刪除其在數(shù)組中的占位。
讓雪花飄落,通過UIView動(dòng)畫完成frame,transform等改變。
當(dāng)動(dòng)畫完成之后,將取出的雪花再次放進(jìn)數(shù)組的尾部

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

  //創(chuàng)建時(shí)鐘,并且添加到主循環(huán)中
  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如何實(shí)現(xiàn)大雪紛飛動(dòng)畫”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁名稱:iOS如何實(shí)現(xiàn)大雪紛飛動(dòng)畫
標(biāo)題來源:http://bm7419.com/article46/pcidhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、微信公眾號(hào)、網(wǎng)站營銷、網(wǎng)站建設(shè)、建站公司App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)