objective-c – NSView drawRect干扰子视图?

objective-c – NSView drawRect干扰子视图?,第1张

概述我有一个nsview,我使用绘制矩形绘制背景图像.它还有3个子视图nsbuttons.问题是,只要鼠标按下按钮,其他按钮就会消失.但是当我删除draw rect方法时,这不会发生.所以我猜这与绘制图像的draw rect方法有关. 我怎么能避免这个? 谢谢. 编辑: 好的,我想出了问题所在.基本上,我有一个NSMenuItem,我用3个按钮在其中放置一个视图.但在NSMenu中,顶部有一个4像素的 我有一个nsvIEw,我使用绘制矩形绘制背景图像.它还有3个子视图nsbuttons.问题是,只要鼠标按下按钮,其他按钮就会消失.但是当我删除draw rect方法时,这不会发生.所以我猜这与绘制图像的draw rect方法有关.

我怎么能避免这个?
谢谢.

编辑:
好的,我想出了问题所在.基本上,我有一个NSMenuItem,我用3个按钮在其中放置一个视图.但在NSMenu中,顶部有一个4像素的填充.所以,基本上,为了删除填充,我使用了这里提供的解决方案:
Gap above NSMenuItem custom view

从解决方案中,drawRect方法中有一行:

[[NSBezIErPath bezIErPathWithRect:fullBounds] setClip];

那一刻,我删除了这一行,按钮表现正常.但是,顶部的填充物不会消失.

这是我的drawRect:

- (voID) drawRect:(NSRect)dirtyRect {    [[NSGraphicsContext currentContext] saveGraphicsstate];    NSRect fullBounds = [self bounds];    fullBounds.size.height += 4;    [[NSBezIErPath bezIErPathWithRect:fullBounds] setClip];    NSImage *background = [NSImage imagenamed:@"bg.png"];    [background drawInRect:fullBounds fromrect:NSZeroRect operation:NSCompositecopy fraction:100.0];    [[NSGraphicsContext currentContext] restoreGraphicsstate];}
解决方法 链接问题的解决方案不包括保存和恢复图形状态,这在您修改未创建的状态时是个好主意.尝试一下:

- (voID)drawRect:(NSRect)dirtyRect {   // Save the current clip rect that has been set up for you   [NSGraphicsContext saveGraphicsstate];   // Calculate your fullBounds rect   // ...   // Set the clip rect   // ...   // Do your drawing   // ...   // Restore the correct clip rect   [NSGraphicsContext restoreGraphicsstate]
总结

以上是内存溢出为你收集整理的objective-c – NSView drawRect干扰子视图?全部内容,希望文章能够帮你解决objective-c – NSView drawRect干扰子视图?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1011724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存