NSImageView 绘制不正确
我创建了一个 NSView 的自定义子类来控制 2 个 NSImageView。创建图像的代码如下:
- (id)initWithFrame:(NSRect)frameRect AndMap:(NSImage *)map AndPlayer:(NSImage *)guyImage
{
self = [super initWithFrame:frameRect];
if (self)
{
NSRect newRect = NSMakeRect(0.0, 0.0, 64.0, 64.0);
NSImageView *theMap = [[NSImageView alloc] initWithFrame:NSMakeRect(0.0, 0.0, frameRect.size.width, frameRect.size.height)];
[theMap setImage:map];
NSImageView *theGuy = [[NSImageView alloc] initWithFrame:newRect];
[theGuy setImage:guyImage];
[self setMapImage:theMap];
[self setPlayerView:theGuy];
[self addSubview:[self mapImage]];
[self addSubview:[self playerView]];
但是,当我告诉视图在原点为 (128,0) 的矩形中绘制时,它会以奇怪的偏移量绘制 theMap ,因此 theGuy 的原点位于 128,0,但 theMap 的原点类似于 (128,20)。我找不到任何理由说明为什么会发生这种情况。
I created a custom subclass of NSView in order to control 2 NSImageViews. The code for creating the images is as follows:
- (id)initWithFrame:(NSRect)frameRect AndMap:(NSImage *)map AndPlayer:(NSImage *)guyImage
{
self = [super initWithFrame:frameRect];
if (self)
{
NSRect newRect = NSMakeRect(0.0, 0.0, 64.0, 64.0);
NSImageView *theMap = [[NSImageView alloc] initWithFrame:NSMakeRect(0.0, 0.0, frameRect.size.width, frameRect.size.height)];
[theMap setImage:map];
NSImageView *theGuy = [[NSImageView alloc] initWithFrame:newRect];
[theGuy setImage:guyImage];
[self setMapImage:theMap];
[self setPlayerView:theGuy];
[self addSubview:[self mapImage]];
[self addSubview:[self playerView]];
However, when I tell the view to draw in a rect with origin (128,0), it draws theMap with a weird offsets, so that theGuy's origin is at 128,0, but theMap's origin is at something like (128,20). I can't find any reason why this should occur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是您有重叠的同级视图:
theMap
有框架{{0.0, 0.0}, {frameRect.size.width, frameRect.size.height}}
theGuy
有框架{{0.0, 0.0}, {64.0, 64.0}}
theMap
和theGuy
都是(或者看起来被)添加到同一个超级视图,即self
,并且具有相同的起源。Cocoa 不保证重叠同级视图的正确行为,即两个视图具有相同的直接父视图并且其相应的帧重叠。来自 查看编程指南:
根据这个答案对另一个问题Stack Overflow,一种替代解决方案是打开视图的图层。
My guess is that you have overlapping sibling views:
theMap
has frame{{0.0, 0.0}, {frameRect.size.width, frameRect.size.height}}
theGuy
has frame{{0.0, 0.0}, {64.0, 64.0}}
theMap
andtheGuy
are (or seem to be) added to the same superview, namelyself
, and have the same origin.Cocoa doesn’t guarantee correct behaviour for overlapping sibling views, i.e., two views that have the same direct superview and whose corresponding frames overlap. From the View Programming Guide:
According to this answer to another question on Stack Overflow, one alternative solution is to turn on layers for the views.
快速思考 - 奇怪的地图偏移是否意外地成为地图
NSImage
的一部分?Quick thought - are the strange map offsets not accidentally part of the map
NSImage
?