在从 iOS6 升级到 iOS7 的过程中,我想我发现了 CATiledLayer 的一个错误,我希望在提交给 Apple 之前让社区对其进行验证。
问题是,如果您有一个 UIScrollView,其中包含许多 CATiledLayers,则这些图块最终将停止更新。
我有一个示例项目在这里展示了这个问题:
https://github.com/sbudhram/CATiledLayerBug
请在运行 iOS6 和 iOS7 的 iPad 上下载并运行。
该项目在 UIScrollView 中生成 900 个 CATiledLayers,分辨率为 3 级。随着用户放大,图块会更新为更精细的分辨率。该代码适用于 iOS6,但瓷砖最终在 iOS7 上停止更新。
我用谷歌搜索,看看是否有人遇到过类似的问题,发现了这个:
但这是不同的,因为我相信这可以在没有内存警告的情况下发生。
这是 UIViewController 中的相关代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.backgroundColor = [UIColor yellowColor];
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 4.1;
_scrollView.zoomScale = 2;
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
self.contentView = [[UIView alloc] initWithFrame:_scrollView.bounds];
_contentView.backgroundColor = [UIColor lightGrayColor];
[_scrollView addSubview:_contentView];
CGFloat tileSize = 20.0f;
CGFloat tileSpacing = 4.0f;
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
CATiledLayer *tLayer = [CATiledLayer layer];
tLayer.bounds = CGRectMake(0, 0, tileSize, tileSize);
tLayer.position = CGPointMake(tileSize/2 + i*(tileSpacing+tileSize), tileSize/2 + j*(tileSpacing+tileSize));
tLayer.delegate = self;
tLayer.contentsGravity = kCAGravityResize;
tLayer.contentsScale = [[UIScreen mainScreen] scale];
tLayer.masksToBounds = NO;
tLayer.opacity = 1.0f;
tLayer.backgroundColor = [UIColor colorWithRed:.2 green:.2 blue:.8 alpha:.5].CGColor;
tLayer.levelsOfDetail = 3;
tLayer.levelsOfDetailBias = 3;
tLayer.tileSize = CGSizeMake(1024., 1024.);
[_contentView.layer addSublayer:tLayer];
}
}
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(@"Zoom: %f", scrollView.zoomScale);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _contentView;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
UIImage *drawImage = nil;
if (_scrollView.zoomScale < 2) {
drawImage = [UIImage imageNamed:@"low.png"];
NSLog(@"Drawing - Low");
}
else if (_scrollView.zoomScale < 4) {
drawImage = [UIImage imageNamed:@"med.png"];
NSLog(@"Drawing - Med");
}
else {
drawImage = [UIImage imageNamed:@"high.png"];
NSLog(@"Drawing - Hi");
}
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -layer.bounds.size.height);
CGContextDrawImage(ctx, layer.bounds, [drawImage CGImage]);
}
这是iOS7上发生的事情的快照(iOS6上所有内容都填写了):