ios中播放gif动画
生活随笔
收集整理的這篇文章主要介紹了
ios中播放gif动画
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?iPhone SDK提供了多種動畫手段,UIView、UIImageView和CALayer都支持動畫。但如何處理常見的gif動畫呢?UIWebView提供了答案,代碼如下:
1.?使用UIWebView播放
????// 設(shè)定位置和大小
????CGRect frame = CGRectMake(50,50,0,0);
????frame.size = [UIImage imageNamed:@"guzhang.gif"].size;
????// 讀取gif圖片數(shù)據(jù)
????NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"guzhang" ofType:@"gif"]];
????// view生成
????UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
????webView.userInteractionEnabled = NO;//用戶不可交互
????[webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
????[self.view addSubview:webView];
????[webView release];
2.將gif圖片分解成多張png圖片,使用UIImageView播放。
代碼如下:
?UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
????NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"],
??????????????????????????????????????????????????[UIImage imageNamed:@"2"],
??????????????????????????????????????????????????[UIImage imageNamed:@"3"],
??????????????????????????????????????????????????[UIImage imageNamed:@"4"],
??????????????????????????????????????????????????[UIImage imageNamed:@"5"],
??????????????????????????????????????????????????[UIImage imageNamed:@"6"],
??????????????????????????????????????????????????[UIImage imageNamed:@"7"],
??????????????????????????????????????????????????[UIImage imageNamed:@"8"],
??????????????????????????????????????????????????[UIImage imageNamed:@"9"],
??????????????????????????????????????????????????[UIImage imageNamed:@"10"],
??????????????????????????????????????????????????[UIImage imageNamed:@"11"],
??????????????????????????????????????????????????[UIImage imageNamed:@"12"],
??????????????????????????????????????????????????[UIImage imageNamed:@"13"],
??????????????????????????????????????????????????[UIImage imageNamed:@"14"],
??????????????????????????????????????????????????[UIImage imageNamed:@"15"],
??????????????????????????????????????????????????[UIImage imageNamed:@"16"],
??????????????????????????????????????????????????[UIImage imageNamed:@"17"],
??????????????????????????????????????????????????[UIImage imageNamed:@"18"],
??????????????????????????????????????????????????[UIImage imageNamed:@"19"],
??????????????????????????????????????????????????[UIImage imageNamed:@"20"],
??????????????????????????????????????????????????[UIImage imageNamed:@"21"],
??????????????????????????????????????????????????[UIImage imageNamed:@"22"],nil];
????gifImageView.animationImages = gifArray; //動畫圖片數(shù)組
????gifImageView.animationDuration = 5; //執(zhí)行一次完整動畫所需的時長
????gifImageView.animationRepeatCount = 1;??//動畫重復(fù)次數(shù)
????[gifImageView startAnimating];
????[self.view addSubview:gifImageView];
????[gifImageView release];
注意:這個方法,如果gif動畫每楨間的時間間隔不同,不能達到此效果。
1.?使用UIWebView播放
????// 設(shè)定位置和大小
????CGRect frame = CGRectMake(50,50,0,0);
????frame.size = [UIImage imageNamed:@"guzhang.gif"].size;
????// 讀取gif圖片數(shù)據(jù)
????NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"guzhang" ofType:@"gif"]];
????// view生成
????UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
????webView.userInteractionEnabled = NO;//用戶不可交互
????[webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
????[self.view addSubview:webView];
????[webView release];
2.將gif圖片分解成多張png圖片,使用UIImageView播放。
代碼如下:
?UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
????NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"],
??????????????????????????????????????????????????[UIImage imageNamed:@"2"],
??????????????????????????????????????????????????[UIImage imageNamed:@"3"],
??????????????????????????????????????????????????[UIImage imageNamed:@"4"],
??????????????????????????????????????????????????[UIImage imageNamed:@"5"],
??????????????????????????????????????????????????[UIImage imageNamed:@"6"],
??????????????????????????????????????????????????[UIImage imageNamed:@"7"],
??????????????????????????????????????????????????[UIImage imageNamed:@"8"],
??????????????????????????????????????????????????[UIImage imageNamed:@"9"],
??????????????????????????????????????????????????[UIImage imageNamed:@"10"],
??????????????????????????????????????????????????[UIImage imageNamed:@"11"],
??????????????????????????????????????????????????[UIImage imageNamed:@"12"],
??????????????????????????????????????????????????[UIImage imageNamed:@"13"],
??????????????????????????????????????????????????[UIImage imageNamed:@"14"],
??????????????????????????????????????????????????[UIImage imageNamed:@"15"],
??????????????????????????????????????????????????[UIImage imageNamed:@"16"],
??????????????????????????????????????????????????[UIImage imageNamed:@"17"],
??????????????????????????????????????????????????[UIImage imageNamed:@"18"],
??????????????????????????????????????????????????[UIImage imageNamed:@"19"],
??????????????????????????????????????????????????[UIImage imageNamed:@"20"],
??????????????????????????????????????????????????[UIImage imageNamed:@"21"],
??????????????????????????????????????????????????[UIImage imageNamed:@"22"],nil];
????gifImageView.animationImages = gifArray; //動畫圖片數(shù)組
????gifImageView.animationDuration = 5; //執(zhí)行一次完整動畫所需的時長
????gifImageView.animationRepeatCount = 1;??//動畫重復(fù)次數(shù)
????[gifImageView startAnimating];
????[self.view addSubview:gifImageView];
????[gifImageView release];
注意:這個方法,如果gif動畫每楨間的時間間隔不同,不能達到此效果。
總結(jié)
以上是生活随笔為你收集整理的ios中播放gif动画的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 写给设计师同学的xcode使用教程: 教
- 下一篇: XCode环境变量及路径设置