我想自定义 NSSliderCell,但我遇到了奇怪的故障。
这是它的样子(放大):
它画得很好,但是当我给旋钮下药时,左栏开始看起来很奇怪(看起来旋钮的部分阴影是从前一帧留下的):
一旦我停止给旋钮下药,滑块就会再次正常。
这是代码:
@implementation VolumeSliderCell
NSImage *knobImage = nil;
NSImage *leftBarLeftCapImage = nil;
NSImage *leftBarFillImage = nil;
NSImage *rightBarFillImage = nil;
NSImage *rightBarRightCapImage = nil;
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
knobImage = [NSImage imageNamed:@"volume_knob.png"];
leftBarLeftCapImage = [NSImage imageNamed:@"volume_leftbar_leftcap.png"];
leftBarFillImage = [NSImage imageNamed:@"volume_leftbar_fill.png"];
rightBarFillImage = [NSImage imageNamed:@"volume_rightbar_fill.png"];
rightBarRightCapImage = [NSImage imageNamed:@"volume_rightbar_rightcap.png"];
}
return self;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
//NSRect cellFrame = [self controlView].bounds;
//[[self controlView] setNeedsDisplayInRect:cellFrame];
[self drawBarInside:cellFrame flipped:[controlView isFlipped]];
[self drawKnob:cellFrame];
}
- (void)drawKnob:(NSRect)knobRect
{
[NSGraphicsContext saveGraphicsState];
CGRect bounds = [self controlView].bounds;
CGFloat value = ([self doubleValue] - [self minValue])/ ([self maxValue] - [self minValue]);
CGRect rect = CGRectMake(value * (bounds.size.width - [knobImage size].width), (bounds.size.height - [knobImage size].height) / 2, [knobImage size].width, [knobImage size].height);
[knobImage drawInRect:CGRectIntegral(rect)];
[NSGraphicsContext restoreGraphicsState];
}
- (void)drawBarInside:(NSRect)aRect flipped:(BOOL)flipped
{
[NSGraphicsContext saveGraphicsState];
CGRect bounds = [self controlView].bounds;
CGFloat value = ([self doubleValue] - [self minValue])/ ([self maxValue] - [self minValue]);
CGRect leftRect = CGRectMake([knobImage size].width / 2, (bounds.size.height - [leftBarFillImage size].height) / 2, value * (bounds.size.width - [knobImage size].width), [leftBarFillImage size].height);
CGRect rightRect = CGRectMake(leftRect.origin.x + leftRect.size.width, (bounds.size.height - [leftBarFillImage size].height) / 2, bounds.size.width - (leftRect.origin.x + leftRect.size.width + [knobImage size].width / 2), [leftBarFillImage size].height);
if (rightRect.size.width < 0)
{
rightRect.size.width = 0;
}
NSDrawThreePartImage(CGRectIntegral(leftRect), leftBarLeftCapImage, leftBarFillImage, nil, NO, NSCompositeSourceOver, 1.0, YES);
NSDrawThreePartImage(CGRectIntegral(rightRect), nil, rightBarFillImage, rightBarRightCapImage, NO, NSCompositeSourceOver, 1.0, YES);
[NSGraphicsContext restoreGraphicsState];
}
怎么了?