题
我已经完成了一个可以使用滚动条和旋钮图像来绘制的自定义滑动单元格。现在,唯一的障碍是,当我快速拖动旋钮时,图像被弄乱了。我发布了屏幕截图。
这是代码:
#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];
}
啊,我好近。关于为什么会发生这种情况以及如何解决的任何帮助将不胜感激,谢谢!
解决方案
好的,所以已经弄清楚了。显然,滑块试图变得聪明,只绘制旋钮的位置。因此,显然,我必须通过在滑块类中覆盖setneedsdisplayinect来始终无效rect。
#import "customSlider.h"
@implementation customSlider
-(void)setNeedsDisplayInRect:(NSRect)invalidRect{
[super setNeedsDisplayInRect:[self bounds]];
}
@end
其他提示
我是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];
对不起,英语不好。
删除所有 -lockFocus
和 -unlockFocus
消息。该框架将在为您设置图纸上下文 -drawBarInside:flipped:
或者 -drawKnob:
曾经发送过。
另外,您不应在Draw方法中创建任何对象。
哈,这是另一个故事。不,nsresponder是正确的,您应该删除所有“锁定焦点”的东西,但是,此问题是NSSlidercell在某处绘制的默认滑块栏的结果 外部 Drawbarinside:翻转:翻转。我也不在迄今为止遇到了这个问题。
这是讨论和解决方案: http://www.cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-bar-from-drom-draming.html 简而言之,您可以覆盖整个drawcell:inview:或使用私有方法使用“肮脏的黑客技巧”。我个人不喜欢黑客,但是在这种情况下我确实
- (BOOL)_usesCustomTrackImage {
return YES;
}
它为我解决了问题
不隶属于 StackOverflow