在从 iOS6 升级到 iOS7 的过程中,我想我发现了 CATiledLayer 的一个错误,我希望在提交给 Apple 之前让社区对其进行验证。
问题在于,如果您的 UIScrollView 中包含许多 CATiledLayers,则图 block 最终将停止更新。
我有一个示例项目在这里展示了这个问题:
https://github.com/sbudhram/CATiledLayerBug
请在运行 iOS6 和 iOS7 的 iPad 上下载并运行。
该项目在一个 UIScrollView 中生成 900 个 CATiledLayers,分辨率为 3 级。随着用户放大,图 block 会更新为更精细的分辨率。该代码适用于 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)scrollViewDidZoomUIScrollView *)scrollView {
NSLog(@"Zoom: %f", scrollView.zoomScale);
}
- (UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView {
return _contentView;
}
- (void)drawLayerCALayer *)layer inContextCGContextRef)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 上的所有内容均已填写):
正如 gavi 所说,这现在可以在 7.1 中使用。
关于ios - CATiledLayer,iOS7 磁贴未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19268069/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |