国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現大雪紛飛動畫

2019-10-21 18:40:57
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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];  }];}

4.Demo也不能少

下載地址:snow

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黎平县| 鲁甸县| 屏东县| 泗水县| 武隆县| 吉安县| 顺昌县| 宁津县| 泽州县| 余姚市| 武陟县| 沙洋县| 固镇县| 金乡县| 得荣县| 康定县| 新乡市| 宣城市| 二手房| 上林县| 鄯善县| 雷波县| 山阳县| 铜山县| 当雄县| 固阳县| 金门县| 岳阳县| 边坝县| 得荣县| 炎陵县| 顺昌县| 图片| 岳阳市| 乐陵市| 定边县| 绥滨县| 凌云县| 乐都县| 沙湾县| 中西区|