您的位置:首页 > 其它

导航控制器在pushViewController时的动画卡顿问题

2015-07-17 16:50 417 查看
昨天在调试导航控制器的时候发现在push的时候动画有卡顿的现象,出现卡顿问题的代码如下:

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


  一开始以为是电脑性能问题,就没在管它,今天早上再次调试的时候还是有这个问题,因为这次切换后的控制器是UITableViewController,重新运行后发现卡顿现象没了,代码如下:

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


然后就对这个问题来兴趣了,为什么切换成UIViewController时会有卡顿的问题呢?先对比一下UITableViewController和UIViewController的不同之处,UITableViewController的View是一个列表,背景色默认为白色,UIViewController的View时空白的,背景显示黑色。背景显示黑色一般有两个原因:

1、背景色是黑色的。

2、UIColor的alpha值是0。

难道UIViewController的View默认是黑色的?先来验证一下。

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


控制条输出结果是:

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


UIViewController的View的color属性是空的,很明显,背景显示黑色的原因是因为颜色是透明的。

UITableViewController的验证结果如下:

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


输出结果:

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


所以,导致卡顿的的罪魁祸首就是UIViewController的View的默认color为空,背景色是透明的。这其实不是卡顿,而是由于透明颜色重叠后视觉上的问题,设置一个背景色就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: