欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年06月22日 10:07:02   作者:桂雛菊  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)大雪紛飛動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)大雪紛飛動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下

1.結(jié)果展示

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

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

4.Demo也不能少

下載地址:snow

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ios 獲取或修改網(wǎng)頁上的內(nèi)容

    ios 獲取或修改網(wǎng)頁上的內(nèi)容

    UIWebView是iOS最常用的SDK之一,它有一個(gè)stringByEvaluatingJavaScriptFromString方法可以將javascript嵌入頁面中,通過這個(gè)方法我們可以在iOS中與UIWebView中的網(wǎng)頁元素交互
    2016-12-12
  • iOS11 WKWebView問題匯總

    iOS11 WKWebView問題匯總

    本文給大家分享的是作者在IOS11中使用WKWebView加載URL出現(xiàn)無法加載內(nèi)容的情況的解決方法,有同樣問題的小伙伴可以查看下
    2017-11-11
  • 詳解IOS宏與常量的使用(define,const)

    詳解IOS宏與常量的使用(define,const)

    這篇文章主要介紹了詳解IOS宏define與常量const的使用方法,適合IOS程序員參考,一起來學(xué)習(xí)下。
    2017-12-12
  • iOS 下的圖片處理與性能優(yōu)化詳解

    iOS 下的圖片處理與性能優(yōu)化詳解

    這篇文章主要介紹了iOS 下的圖片處理與性能優(yōu)化詳解,幫助大家更好的理解和學(xué)習(xí)使用ios開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • iOS Moya實(shí)現(xiàn)OAuth請(qǐng)求的方法

    iOS Moya實(shí)現(xiàn)OAuth請(qǐng)求的方法

    這篇文章主要介紹了iOS Moya實(shí)現(xiàn)OAuth請(qǐng)求的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • iOS實(shí)現(xiàn)抖音點(diǎn)贊動(dòng)畫效果

    iOS實(shí)現(xiàn)抖音點(diǎn)贊動(dòng)畫效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)抖音點(diǎn)贊動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • searchDisplayController 引起的數(shù)組越界處理辦法

    searchDisplayController 引起的數(shù)組越界處理辦法

    這篇文章主要介紹了searchDisplayController 引起的數(shù)組越界處理辦法,需要的朋友可以參考下
    2015-07-07
  • iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法

    iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法

    這篇文章主要介紹了iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • iOS消息遠(yuǎn)程推送通知

    iOS消息遠(yuǎn)程推送通知

    這篇文章主要為大家詳細(xì)介紹了iOS消息遠(yuǎn)程推送通知代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 提高iOS開發(fā)的小技巧和思路小結(jié) (二)

    提高iOS開發(fā)的小技巧和思路小結(jié) (二)

    這篇文章主要跟大家分享了關(guān)于提高iOS開發(fā)的一些小技巧和思路,通過本文總結(jié)的這些小技巧和思路相信對(duì)對(duì)大家開發(fā)iOS具有一定的參考價(jià)值,感興趣的朋友們可以參考學(xué)習(xí),下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04

最新評(píng)論