2011-05-29 38 views
0

我创建了NSView的自定义子类以控制2个NSImageViews。创建图像的代码如下: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's origin is at 128,0,but the map's origin is at something like(128,20)。我找不到为什么会发生这种情况。

回答

2

我的猜测是,你有重叠的兄弟姐妹的观点:

  • theMap有框架{{0.0, 0.0}, {frameRect.size.width, frameRect.size.height}}
  • theGuy具有框架{{0.0, 0.0}, {64.0, 64.0}}
  • theMaptheGuy是(或者看起来是)添加到相同的上海华,即self,并且具有相同的来源。

可可并不保证重叠同胞视图的正确行为,即两个视图具有相同的直接超视图并且其相应的框架重叠。从View Programming Guide

注:由于性能原因,可可并不强制裁剪同级视图之间或保证正确的失效和图纸的行为,当兄弟的意见重叠。如果您希望在另一个视图前面绘制视图,则应该使前视图成为后视图的子视图(或后代)。

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

0

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