- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 View 有一个 CATiledLayer
。 View Controller 具有自定义缩放行为,因此在 -scrollViewDidEndZooming
上每个图 block 都需要重绘。但是,即使在每次缩放后在图层上调用 -setNeedsDisplay
,也并非所有图 block 都被重绘。这导致 View 在缩放后有时看起来不对。 (应该出现在 1 个图 block 中的东西出现在多个地方)。它通常会在另一次缩放后自行纠正。
这是相关代码。 updatedRects
用于测试——它存储由 -drawLayer
请求绘制的唯一矩形。
Canvas View .h:
@interface CanvasView : UIView
@property (nonatomic, retain) NSMutableSet *updatedRects;
@end
Canvas View .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
测试此功能时,我总是在缩放后一直滚动整个 View ,以确保看到每个图 block 。每当 View 看起来不对时,在下一次调用 -updateCanvas
时,预测的“下一个更新的 rects 数”大于实际的“更新的 rects 数”。什么会导致这个?
编辑:
这里有一个更好的可视化问题的方法。每次调用 updateCanvas
时,我都让它改变绘制图 block 的背景颜色并记录它被调用的时间——颜色和时间存储在 canvasView
中。在每个图 block 上绘制图 block 的矩形、图 block 沿 x 轴的索引、设置背景颜色的时间(秒)以及绘制图 block 的时间(秒)。如果一切正常,所有的瓷砖应该是相同的颜色。相反,这是我有时会看到的:
缩小后我似乎只看到错误的结果。
这个问题可能与
(编辑:否——未多次调用。)scrollViewDidEndZooming
以及
updateCanvas
每次缩小时会被调用多次有关。
最佳答案
有一个相当昂贵的 hack 可能对你有用:
tiledLayer.contents = nil;
[tiledLayer setNeedsDisplayInRect:tiledLayer.bounds];
如果我没记错的话,第一行实际上将平铺层变成了普通层,但第二行又把它转回去了。但是,它会丢弃平铺层中的所有内容。
关于iphone - 在 CATiledLayer -setNeedsDisplay 之后,并非所有图 block 都重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5198155/
在我的代码中,我想要“动画”绘制一条线的延迟,因此在向 View 添加新线后,我调用 setNeedsDisplay - 一次就可以正常工作。 在drawRect方法中,我绘制线条并调用线条的方法来增
我正在尝试使用 MonoTouch 创建自定义 ProgressBar。我创建了一个用计时器重新绘制线条的类。我的问题是,当调用 SetNeedsDisplay 时, View 不会自行重绘。仅当提供
我注意到 Mac OS X 10.7 上基于 Cocoa 的应用程序存在一个奇怪的问题。由于某种原因(这里并不重要),有时我必须在自定义 View 的 drawRect 方法之外进行绘制。 我必须在我
我正在运行一个 Parse 程序,该程序获取数据并使用“findObjectsInBackgroundWithBlock”从中创建对象。完成后,我更新 self ,然后调用“[self.tableVi
每次使用 setNeedsDisplay 调用 drawRect 时,我都会显示大量图像。问题是导致滞后导致每次我调用 dracRect 时,我都会重新绘制每个图像(数千张)。 有没有办法不重绘所有图
下面的方法是在非主线程上调用的,具体来说,是在录音音频队列回调中 - (void)myMethod { //... dispatch_async(dispatch_get_main_queue(),
我最近开始学习在 View 上绘制核心图形,但每次调用 setNeedsDisplay 时,该方法都会运行,但不会触发 drawRect。在我的项目中,我在 View Controller 上有一个
我有一个自定义 View ,其中绘制了一个钢琴键盘。每个键都作为单独的调用绘制,因此我只能绘制需要重绘的键。该 View 支持多点触控,因此可以同时按住多个键。 每个键的绘制成本都有些高,因此每当在
在我的程序中,我使用 slider 更改当前所选颜色的 alpha 级别。 当 slider 更改其值时,它调用我的 setColor() 方法来更新使用新的 alpha 值更新当前颜色并更新显示当前
我正在 UIContainerView 中创建 subview ,它属于下面描述的类。这些值正确地打印到终端窗口,但实际上并没有显示在我定义的 subview 中。我认为 setNeedsDispla
我正在尝试重新加载 UIView。实际上我得到一个错误 Cannot convert value of type 'HomeViewController' to expected argument t
我对 setNeedsDisplay 更新 UIView 的不同用法感到困惑?当我尝试使用: [self.view setNeedsDisplay:YES]; 我发现我必须忽略参数才能使其适用于 UI
这里有人知道 setNeedsDisplay 是否会立即调用 drawRect 并立即更新屏幕吗?或者它只是一个排队的请求?谢谢。 最佳答案 View 直到下一个绘图周期才真正重绘。这只是通知系统应该
我看到很多与此问题相关的线程,但没有一个解决我的问题(我认为)。 我的情况应该很简单,我的 Controller 中有一个自定义 UIView,我在我的 Controller 中使用了 [self.m
使用drawRect、setNeedsDisplay和layoutSubViews等方法有什么基本规则吗?我相信它们会对应用程序性能产生不利影响。这些消息有任何首选替代方案吗? 最佳答案 虽然这不是
我有一个更新 View 的后台任务。该任务调用 -setNeedsDisplay 来绘制 View 。 这有效: - (void) drawChangesTask; { NSAutorelea
我的 ui 结构类似于 NSWindow -> NSViewController -> NSView(parent) -> NSView(target view)。目标 View 是从 XIB 文件中
我正在尝试制作一个非常简单的绘画应用程序(Cocoa)。在我的自定义 View 中,我有我的drawRect:方法,它在光标位置创建一个矩形: - (void)drawRect:(NSRect)rec
我有一个仅绘制的 UIView 派生 View 一次并且从未更新。 调用[self setNeedsDisplay]没有效果。什么可能导致这种情况? 绘图代码是否在无关紧要 drawRect: , 或
我正在尝试创建一个循环进度指示器。不幸的是,仅从 setNeedsDisplay 第一次和最后一次调用 drawRect 例程,它不会创建我正在寻找的渐变填充模式。我创建了一个 UIView 子类,在
我是一名优秀的程序员,十分优秀!