導航控制器在pushViewController時的動畫卡頓問題


昨天在調試導航控制器的時候發現在push的時候動畫有卡頓的現象,出現卡頓問題的代碼如下:

1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
2     UIViewController* newController = [[UIViewController alloc] init];
3     newController.title = @"新的控制器";
4     [self.navigationController pushViewController:newController animated:YES];
5 }

 

  一開始以為是電腦性能問題,就沒在管它,今天早上再次調試的時候還是有這個問題,因為這次切換后的控制器是UITableViewController,重新運行后發現卡頓現象沒了,代碼如下:

1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
2     UITableViewController* newController = [[UITableViewController alloc] init];
3     newController.title = @"新的控制器";
4     [self.navigationController pushViewController:newController animated:YES];
5 }

然后就對這個問題來興趣了,為什么切換成UIViewController時會有卡頓的問題呢?先對比一下UITableViewController和UIViewController的不同之處,UITableViewController的View是一個列表,背景色默認為白色,UIViewController的View時空白的,背景顯示黑色。背景顯示黑色一般有兩個原因:

1、背景色是黑色的。

2、UIColor的alpha值是0。

難道UIViewController的View默認是黑色的?先來驗證一下。

1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
2     UIViewController* newController = [[UIViewController alloc] init];
3     UIColor* color = newController.view.backgroundColor;
4     NSLog(@"Color: %@", color);
5     newController.title = @"新的控制器";
6     [self.navigationController pushViewController:newController animated:YES];
7 }

控制條輸出結果是:

2015-06-04 12:30:17.007 Weibo[5110:607] Color: (null)

UIViewController的View的color屬性是空的,很明顯,背景顯示黑色的原因是因為顏色是透明的。

UITableViewController的驗證結果如下:

1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
2     UITableViewController* newController = [[UITableViewController alloc] init];
3     UIColor* color = newController.view.backgroundColor;
4     NSLog(@"Color: %@", color);
5     newController.title = @"新的控制器";
6     [self.navigationController pushViewController:newController animated:YES];
7 }

輸出結果:

2015-06-04 12:34:12.555 Weibo[5128:607] Color: UIDeviceRGBColorSpace 1 1 1 1

所以,導致卡頓的的罪魁禍首就是UIViewController的View的默認color為空,背景色是透明的。這其實不是卡頓,而是由於透明顏色重疊后視覺上的問題,設置一個背景色就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM