Not all tiles redrawn after CATiledLayer -setNeedsDisplay <p>My view has a <code>CATiledLayer</code>. The view controller has custom zooming behavior, so on <code>-scrollViewDidEndZooming</code> every tile needs to be redrawn. But, even though <code>-setNeedsDisplay</code> is being called on the layer after every zoom, not all tiles are being redrawn. This is causing the view to sometimes look wrong after a zoom. (Things that should appear in just 1 tile are appearing in multiple places). It often corrects itself after another zoom. </p> <p>Here's the relevant code. <code>updatedRects</code> is for testing -- it stores the unique rectangles requested to be drawn by <code>-drawLayer</code>. </p> <p>CanvasView.h: </p> <pre><code>@interface CanvasView : UIView @property (nonatomic, retain) NSMutableSet *updatedRects; @end </code></pre> <p>CanvasView.m: </p> <pre><code>@implementation CanvasView @synthesize updatedRects; + (Class)layerClass { return [CATiledLayer class]; } - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context { CGRect rect = CGContextGetClipBoundingBox(context); [updatedRects addObject:[NSValue valueWithCGRect:rect]]; CGContextSaveGState(context); CGContextTranslateCTM(context, rect.origin.x, rect.origin.y); // ...draw into context... CGContextRestoreGState(context); } @end </code></pre> <p>MyViewController.m: </p> <pre><code>@implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; canvasView.updatedRects = [NSMutableSet set]; } - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { canvasView.transform = CGAffineTransformIdentity; canvasView.frame = CGRectMake(0, 0, canvasView.frame.size.width * scale, canvasView.frame.size.height); [self updateCanvas]; } - (void)updateCanvas { NSLog(@"# rects updated: %d", [canvasView.updatedRects count]); [canvasView.updatedRects removeAllObjects]; canvasView.frame = CGRectMake(canvasView.frame.origin.x, canvasView.frame.origin.y, canvasView.frame.size.width, myHeight); CGFloat tileSize = 256; NSLog(@"next # rects updated will be: %f", [canvasView.layer bounds].size.width / tileSize); [canvasView.layer setNeedsDisplay]; } @end </code></pre> <p>When testing this, I always scrolled all the way across the view after zooming to make sure every tile was seen. Whenever the view looked wrong, the predicted "next # of rects updated" was greater than the actual "# of rects updated" on the next call to <code>-updateCanvas</code>. What would cause this? </p> <p><strong>Edit:</strong> </p> <p>Here's a better way to visualize the problem. Each time <code>updateCanvas</code> is called, I've made it change the background color for drawing tiles and record the time it was called -- the color and time are stored in <code>canvasView</code>. On each tile is drawn the tile's rectangle, the tile's index along the x axis, the time (sec.) when the background color was set, and the time (sec.) when the tile was drawn. If everything is working correctly, all tiles should be the same color. Instead, here's what I'm sometimes seeing: </p> <p><img src="https://i.stack.imgur.com/k2BjY.png" alt="Screenshot of tiles with mismatched colors"> <img src="https://i.stack.imgur.com/6GzNS.png" alt="Screenshot of tiles with mismatched colors"></p> <p>I only seem to see wrong results after zooming out. <strike>The problem might be related to the fact that <code>scrollViewDidEndZooming</code>, and therefore <code>updateCanvas</code>, can get called multiple times per zoom-out.</strike> (<strong>Edit</strong>: No-- not called multiple times.) </p>