加载GiF动画
前言:需求上的需要,UI這邊給了一張GIF動畫,讓做為動圖按鈕加載出來。 加載GIF動畫的辦法有非常多種,下面簡單寫一下自己的做法。
一、使用SDWebImaged的GIF擴展類加載本地Gif文件
#import "gifViewController.h" #import "UIImage+GIF.h"@interface gifViewController () @property (nonatomic, strong) UIImageView *loadingGifImage; @end@implementation gifViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];[self loadingGifImageView]; }- (void)loadingGifImageView{NSString *name = @"gif.gif";NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];NSData *imageData = [NSData dataWithContentsOfFile:filePath];if (!self.loadingGifImage) {self.loadingGifImage = [[UIImageView alloc]init];}self.loadingGifImage.backgroundColor = [UIColor clearColor];self.loadingGifImage.image = [UIImage sd_animatedGIFWithData:imageData];self.loadingGifImage.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth*250.0/750.0);[self.view addSubview:self.loadingGifImage];[self.view bringSubviewToFront:self.loadingGifImage]; } @end二、加載由幀序列組成的本地GIF動畫
#import "gifViewController.h"@interface gifViewController () @property (nonatomic, strong) UIImageView *loadingImageView; @end@implementation gifViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];[self loadingAnimation]; }- (void)loadingAnimation {[self.view addSubview:self.loadingImageView];[self.loadingImageView mas_makeConstraints:^(MASConstraintMaker *make) {make.center.equalTo(self.view);make.size.mas_equalTo(CGSizeMake(40.0, 40.0));}]; }- (UIImageView *)loadingImageView {if (!_loadingImageView) {NSMutableArray *imgArray = [[NSMutableArray alloc] init];for (int i = 0; i<24; i++) {NSString *name = [NSString stringWithFormat:@"loading_%05d@3x.png", i]; //例如:loading_00000@3x.PNGNSString *imageName = [@"LoadingAnimation.bundle" stringByAppendingPathComponent:name];UIImage *image = [UIImage imageNamed:imageName];[imgArray addObject:image];}_loadingImageView = [[UIImageView alloc] init];_loadingImageView.animationImages = imgArray;_loadingImageView.animationDuration = 1.0;[self.loadingImageView startAnimating];}return _loadingImageView; } @end總結(jié)
- 上一篇: (10) 需求征集 -- 权限管理
- 下一篇: 如何在mac系统下搭建git服务器