直接上示例代碼:
#import "MBProgressHUD.h" ?//導(dǎo)入頭文件瘪菌,也可以自己自定義
#import "UIImage+GIF.h" ?//導(dǎo)入SDWebImage中的UIImage+GIF.h文件名党,這個(gè)文件是關(guān)鍵
接下來就是ViewDidLoad中的代碼了:
NSURL *url = [NSURL URLWithString:@"http://ww1.sinaimg.cn/large/85cccab3tw1esjw3dyui5g209q0bk1kx.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage? *image=[UIImage sd_animatedGIFWithData:data];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image =image;
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.color = [UIColor grayColor];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"加載中。截酷。。";
HUD.customView = imageView;
全部的代碼都在這里了(哦乾戏!對(duì)了迂苛,不設(shè)置frame自動(dòng)默認(rèn)是圖片大小三热,貌似UIImageView的大小最小就是圖片的大小,再設(shè)怎么小都沒有用)
這就完成了三幻!
我們也可以用系統(tǒng)自帶的方法來實(shí)現(xiàn)動(dòng)畫效果:
直接上代碼:
// 提前準(zhǔn)備好4張圖片
UIImage *image1 = [UIImage imageNamed:@"/Users/jijiawei/Desktop/image1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"/Users/jijiawei/Desktop/image2.jpg"];
UIImage *image3 = [UIImage imageNamed:@"/Users/jijiawei/Desktop/image3.jpg"];
UIImage *image4 = [UIImage imageNamed:@"/Users/jijiawei/Desktop/image4.jpg"];
// 創(chuàng)建一個(gè)UIImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
// 將圖片方法UIImageView的動(dòng)畫數(shù)組里面
imageView.animationImages = [NSArray arrayWithObjects:image1,image2,image3,image4, nil];
// 設(shè)置UIImageView的動(dòng)畫時(shí)間(動(dòng)畫用時(shí))
imageView.animationDuration = 1.0;
// 永遠(yuǎn)重復(fù)(如果是1就漾,就是1次,以此類推赌髓。从藤。)
imageView.animationRepeatCount = 0;
// 開始動(dòng)畫
[imageView startAnimating];
// 添加到self.View上面
[self.view addSubview:imageView];
以下是全部代碼截圖: