之後重新繪製我的視圖有CATiledLayer
。視圖控制器具有自定義縮放行爲,因此在-scrollViewDidEndZooming
上每個tile都需要重繪。但是,即使在每次縮放後在圖層上調用-setNeedsDisplay
,但並非所有圖塊都正在重繪。這導致視圖在縮放後有時看起來不正確。 (應該只出現在1個貼圖中的東西出現在多個位置)。它經常在另一次縮放後自行糾正。並非所有瓷磚在CATiledLayer -setNeedsDisplay
下面是相關的代碼。 updatedRects
用於測試 - 它存儲請求由-drawLayer
繪製的唯一矩形。
CanvasView.h:
@interface CanvasView : UIView
@property (nonatomic, retain) NSMutableSet *updatedRects;
@end
CanvasView.m:
@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
MyViewController.m:
@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
當測試這一點,我一直滾動所有跨視圖方式放大後確保每個瓷磚都被看到。每當視圖看起來不正確時,預計的「更新的下一個rect」大於下次調用-updateCanvas
時實際的「rects更新次數」。什麼會造成這種情況?
編輯:
這裏有一個更好的方式來可視化的問題。每次調用updateCanvas
時,我都會更改背景顏色以繪製圖塊並記錄調用時間 - 顏色和時間存儲在canvasView
中。在每個圖塊上繪製圖塊的矩形,沿x軸的圖塊索引,設置背景顏色的時間(秒),以及繪製圖塊時的時間(秒)。如果一切正常,所有瓷磚應該是相同的顏色。相反,這裏就是我有時看到:
我只依稀看到縮小後錯誤的結果。
此問題可能與
(編輯:No--不是多次調用。)scrollViewDidEndZooming
,因此
updateCanvas
每次縮小可能被調用多次有關。
這可能與CATiledLayer中的一個錯誤有關:如果您在繪製圖塊的過程中調用「setNeedsDisplay」,則該圖塊將不會重繪。 – fishinear 2012-08-02 10:07:20