一,UIWebview加載
核心代碼如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
#pragma mark- 使用uiwebview加載gif
/*
NSData *data = [NSData dataWithContentsOfFile:path];
使用loadData:MIMEType:textEncodingName: 則有警告
[webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
*/
webView = [[UIWebView alloc] initWithFrame:CGRectZero];
webView.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"earth1" ofType:@"gif"];
NSURL *url = [NSURL URLWithString:path];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:webView];
//加載xib
aaView = [LoginView loadLoginView];
aaView.backgroundColor = [UIColor clearColor];
[aaView setFrame:self.view.bounds];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView setFrame:self.view.bounds];
[self.view addSubview:aaView];
}
注意:
上面的xib也就是,登錄自定義view要在webview請求完之后再加上去,否則會出現(xiàn)白屏一閃之后再到正常。(原因其實(shí)很簡單,webview請求數(shù)據(jù)是需要點(diǎn)時(shí)間的)
還有使用webview加載gif,無法設(shè)置動畫間隔時(shí)間
二,SDWebImage加載
注意SDWebImage4.0之后,加載gif方式的更新,新方法如下截圖:

示例代碼:
#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"
FLAnimatedImageView *imgView = [FLAnimatedImageView new];
[view addSubview:imgView];
NSString* filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loading" ofType:@"gif"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
imgView.backgroundColor = [UIColor clearColor];
imgView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
其它方法
一些第三方框架等
效果圖:

圖片來自網(wǎng)絡(luò),侵權(quán)立刪