NSImageView 绘制不正确

发布于 2024-11-10 07:20:34 字数 891 浏览 4 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-11-17 07:20:35

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

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

Cocoa 不保证重叠同级视图的正确行为,即两个视图具有相同的直接父视图并且其相应的帧重叠。来自 查看编程指南

注意:出于性能原因,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}}
  • both theMap and theGuy are (or seem to be) added to the same superview, namely self, 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:

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.

According to this answer to another question on Stack Overflow, one alternative solution is to turn on layers for the views.

落墨 2024-11-17 07:20:35

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

Quick thought - are the strange map offsets not accidentally part of the map NSImage?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文