这是相关的代码. 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,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
测试时,我总是在缩放后一直在视图中滚动,以确保看到每个图块.每当视图看起来错误时,预测的“下一个更新的数量”大于下次调用-updateCanvas时实际的“更新的数量”.什么会导致这个?
编辑:
这是一种可视化问题的更好方法.每次调用updateCanvas时,我都会更改绘制切片的背景颜色并记录它被调用的时间 – 颜色和时间存储在canvasVIEw中.在每个图块上绘制图块的矩形,沿x轴的图块索引,设置背景颜色的时间(秒)以及图块绘制时的时间(秒).如果一切正常,所有瓷砖应该是相同的颜色.相反,这是我有时会看到的:
在缩小后我似乎只看到错误的结果.问题可能与scrollVIEwDIDEndZooming和updateCanvas每次缩小多次调用的事实有关. (编辑:不 – 不多次调用.)
解决方法 有一个相当昂贵的黑客可能对你有用:tiledLayer.contents = nil;[tiledLayer setNeedsdisplayInRect:tiledLayer.bounds];
如果我没记错的话,第一行实际上将平铺图层转换为普通图层,但第二行将其转回.但是,它会将所有内容都丢弃在平铺层中.
总结以上是内存溢出为你收集整理的iphone – 在CATiledLayer -setNeedsDisplay之后不是所有的瓷砖重新绘制全部内容,希望文章能够帮你解决iphone – 在CATiledLayer -setNeedsDisplay之后不是所有的瓷砖重新绘制所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)