gpt4 book ai didi

objective-c - NSImageView 绘制不正确

转载 作者:行者123 更新时间:2023-12-03 17:43:14 25 4
gpt4 key购买 nike

我创建了一个 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]];

但是,当我告诉 View 在原点为 (128,0) 的矩形中绘制时,它会以奇怪的偏移量绘制 theMap ,因此 theGuy 的原点位于 128,0,但theMap 的原点类似于 (128,20)。我找不到发生这种情况的任何原因。

最佳答案

我的猜测是您有重叠的同级 View :

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

Cocoa 不保证重叠同级 View 的正确行为,即两个 View 具有相同的直接父 View 并且其相应的帧重叠。来自 View Programming Guide :

Note: For performance reasons, Cocoa does not enforce clipping among sibling views or guarantee correct invalidation and drawing behavior when sibling views overlap. If you want a view to be drawn in front of another view, you should make the front view a subview (or descendant) of the rear view.

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

关于objective-c - NSImageView 绘制不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6165615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com