我創建了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)。我找不到爲什麼會發生這種情況。