NSImageView绘图不正确

时间:2011-05-29 02:29:06

标签: objective-c cocoa nsimageview

我创建了NSView的自定义子类,以便控制2个NSImageViews。创建图像的代码如下:

- (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)。我找不到任何理由为什么会发生这种情况。

2 个答案:

答案 0 :(得分:2)

我的猜测是你有重叠的兄弟视图:

  • theMap有框架{{0.0, 0.0}, {frameRect.size.width, frameRect.size.height}}
  • theGuy有框架{{0.0, 0.0}, {64.0, 64.0}}
  • theMaptheGuy都被(或似乎)添加到同一个超级视图中,即self,并且具有相同的来源。

Cocoa不保证重叠的兄弟视图的正确行为,即具有相同直接超视图且其相应帧重叠的两个视图。来自View Programming Guide

  

注意:出于性能原因,Cocoa不会在同级视图中强制执行剪切,也不会在兄弟视图重叠时保证正确的失效和绘制行为。如果要在另一个视图前绘制视图,则应将前视图设为后视图的子视图(或后代)。

根据this answer to another question on Stack Overflow,另一种解决方案是打开视图的图层。

答案 1 :(得分:0)

快速思考 - 奇怪的地图偏移是不是意外地成为地图的一部分NSImage

相关问题