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; //動畫重復次數(shù) [gifImageView startAnimating]; [self.view addSubview:gifImageView]; [gifImageView release]; |
|