自定义 NSSliderCell
我已经实现了一个自定义滑块单元,可以使用滚动条和旋钮的图像进行绘制。现在唯一的障碍是,当我快速拖动旋钮时,图像会变得混乱。我已经发布了屏幕截图。
这是代码:
#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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除所有
-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.
哈,那是另一个故事了。不,NSResponder 是对的,您应该删除所有“lockFocus”内容,但是,此问题是由 NSSliderCell 在drawBarInside:flipped:flipped 的外部某处绘制的默认滑块造成的。不久前我也遇到过这个问题。
这是讨论和解决方案: http://www .cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-from-drawing.html,简而言之,您可以覆盖整个drawCell:inView:或使用“肮脏的黑客技巧”来覆盖私有方法。我个人不喜欢黑客,但在这种情况下我喜欢
它为我解决了问题
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
And it solved the problem for me
好的,所以已经弄清楚了。显然,滑块试图变得聪明,只在旋钮所在的位置绘制。所以显然我必须通过覆盖滑块类中的 setNeedsDisplayInRect 来始终使矩形无效。
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.
我是 Objective-c 的初学者。
我也遇到了这个问题!这是我花了两天时间找到的解决方案)))
保存和恢复 GraphicsState:
抱歉英语不好。
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:
Sorry for bad English.