自定义 NSSliderCell

发布于 2024-09-28 17:32:51 字数 1227 浏览 4 评论 0原文

我已经实现了一个自定义滑块单元,可以使用滚动条和旋钮的图像进行绘制。现在唯一的障碍是,当我快速拖动旋钮时,图像会变得混乱。我已经发布了屏幕截图。

Screenshot

这是代码:

#import "customSliderCell.h"
@implementation customSliderCell
- (void)drawKnob:(NSRect)knobRect {

    NSImage * knob = knobImage;
 [[self controlView] lockFocus];
 [knob
 compositeToPoint:NSMakePoint(knobRect.origin.x,knobRect.origin.y+knobRect.size.height)
 operation:NSCompositeSourceOver];

[[self controlView] unlockFocus];
}
- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped {
rect.size.height = 8;

    NSRect leftRect = rect;
    leftRect.origin.x=0;
    leftRect.origin.y=2;
    leftRect.size.width = knobrect.origin.x + (knobrect.size.width);
    [leftBarImage setSize:leftRect.size];
    [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];

    NSRect rightRect = rect;
    rightRect.origin.x=0;
    rightRect.origin.y=2;
    rightRect.origin.x = knobrect.origin.x;
    [rightBarImage setSize:rightRect.size];
    [rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
}

啊,我已经很接近了。任何关于为什么会发生这种情况以及如何解决它的帮助将不胜感激,谢谢!

I've sort of accomplised implementing a custom slider cell that can draw over using images for the scroll bar and knob. The only obstacle that is in the way now is this, when I drag the knob quickly, the images get messed up. I've posted a screen shot.

Screenshot

Here is the code:

#import "customSliderCell.h"
@implementation customSliderCell
- (void)drawKnob:(NSRect)knobRect {

    NSImage * knob = knobImage;
 [[self controlView] lockFocus];
 [knob
 compositeToPoint:NSMakePoint(knobRect.origin.x,knobRect.origin.y+knobRect.size.height)
 operation:NSCompositeSourceOver];

[[self controlView] unlockFocus];
}
- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped {
rect.size.height = 8;

    NSRect leftRect = rect;
    leftRect.origin.x=0;
    leftRect.origin.y=2;
    leftRect.size.width = knobrect.origin.x + (knobrect.size.width);
    [leftBarImage setSize:leftRect.size];
    [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];

    NSRect rightRect = rect;
    rightRect.origin.x=0;
    rightRect.origin.y=2;
    rightRect.origin.x = knobrect.origin.x;
    [rightBarImage setSize:rightRect.size];
    [rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
}

ah i'm so close. any help regarding as to why this happens and how to solve it will be greatly appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

流绪微梦 2024-10-05 17:32:52

删除所有 -lockFocus-unlockFocus 消息。框架将在发送 -drawBarInside:flipped:-drawKnob: 之前为您设置绘图上下文。

另外,您不应该在绘制方法中创建任何对象。

Remove all the -lockFocus and -unlockFocus messages. The framework will take care of setting up the drawing context for you before -drawBarInside:flipped: or -drawKnob: are ever sent.

Also, you shouldn't be creating any objects within a draw method.

吃兔兔 2024-10-05 17:32:52

哈,那是另一个故事了。不,NSResponder 是对的,您应该删除所有“lockFocus”内容,但是,此问题是由 NSSliderCell 在drawBarInside:flipped:flipped 的外部某处绘制的默认滑块造成的。不久前我也遇到过这个问题。

这是讨论和解决方案: http://www .cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-from-drawing.html,简而言之,您可以覆盖整个drawCell:inView:或使用“肮脏的黑客技巧”来覆盖私有方法。我个人不喜欢黑客,但在这种情况下我喜欢

- (BOOL)_usesCustomTrackImage {
return YES;
}

它为我解决了问题

Ha, it's another story. No, NSResponder is right and you should remove all 'lockFocus' stuff, however, this issue is a result of the default slider bar drawn by the NSSliderCell somewhere outside of the drawBarInside:flipped:flipped. I have faced this issue not so far ago as well.

Here is discussion and solution: http://www.cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-from-drawing.html , in short, you can override whole drawCell:inView: or use a "dirty hack trick" with overriding a private method. I personally don't like hacks, but in this case I did

- (BOOL)_usesCustomTrackImage {
return YES;
}

And it solved the problem for me

仅冇旳回忆 2024-10-05 17:32:51

好的,所以已经弄清楚了。显然,滑块试图变得聪明,只在旋钮所在的位置绘制。所以显然我必须通过覆盖滑块类中的 setNeedsDisplayInRect 来始终使矩形无效。

#import "customSlider.h"

@implementation customSlider
-(void)setNeedsDisplayInRect:(NSRect)invalidRect{
    [super setNeedsDisplayInRect:[self bounds]];
}
@end

Ok, so it's figured out. apparently the slider was trying to be smart and draw only where the knob has been. so apparently I have to invalidate the rect all the time by overriding setNeedsDisplayInRect in the slider class.

#import "customSlider.h"

@implementation customSlider
-(void)setNeedsDisplayInRect:(NSRect)invalidRect{
    [super setNeedsDisplayInRect:[self bounds]];
}
@end
我一直都在从未离去 2024-10-05 17:32:51

我是 Objective-c 的初学者。
我也遇到了这个问题!这是我花了两天时间找到的解决方案)))
保存和恢复 GraphicsState:

[NSGraphicsContext restoreGraphicsState];
//...
[leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
//...
[rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
[NSGraphicsContext saveGraphicsState];

抱歉英语不好。

I am a beginner in Objective-c.
I also ran into this problem! Here is the solution to find that I spent two days)))
Save and restore GraphicsState:

[NSGraphicsContext restoreGraphicsState];
//...
[leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
//...
[rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
[NSGraphicsContext saveGraphicsState];

Sorry for bad English.

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