CATiledLayer,iOS7磁贴不更新

时间:2013-10-09 09:28:32

标签: ios uiscrollview ios7 catiledlayer

在从iOS6升级到iOS7时,我想我发现了CATiledLayer的一个错误,我希望在提交给Apple之前让社区验证它。

问题是如果你的UIScrollView里面有很多CATiledLayers,那么这些图块最终会停止更新。

我有一个展示此问题的示例项目:

https://github.com/sbudhram/CATiledLayerBug

请在运行iOS6和iOS7的iPad上下载并运行。

该项目在UIScrollView中生成900个CATiledLayers,分辨率为3级。当用户放大时,图块会更新为更精细的分辨率。该代码适用于iOS6,但瓷砖最终会停止在iOS7上更新。

我已经四处搜索,看看是否有人遇到类似的问题,并发现了这一点:

  

http://www.cocoanetics.com/2013/09/welcome-to-ios-7-issues/

这是不同的,因为我相信这可以在没有记忆警告的情况下发生。

以下是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上填写了所有内容):

iOS7 Bug

1 个答案:

答案 0 :(得分:1)

如gavi所述,现在可以在7.1中使用。