自定义NSProgressIndicator drawrect总是在隐藏时调用

时间:2013-12-05 12:42:34

标签: cocoa customization drawrect nsprogressindicator

我的自定义ProgressIndicator的-drawrect方法在启动时调用一次,在复制过程中多次调用,没关系。但是在复制进度之后,当隐藏进度指示器时,进度指示器也会一直重绘。

@implementation NewProgressBar

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        indeterminate_X_1 = 0;
        indeterminate_X_2 = 0;
        indeterminate_X_3 = 0;
    }
    return self;
}

-(void)setDoubleValue:(double)value {
    [super setDoubleValue:value];
    if (![self isDisplayedWhenStopped] && value == [self maxValue]) {
        [self stopAnimation:self];
    }
}

- (void)startAnimation:(id)sender
{
    if (!animator) {
        animator = [NSTimer scheduledTimerWithTimeInterval:1.0/120 target:self selector:@selector(activateAnimation:) userInfo:nil repeats:YES];
    }
}

-(void)activateAnimation:(NSTimer*)timer {
    [self setNeedsDisplay:YES];
}

-(void)stopAnimation:(id)sender
{
    animator = nil;
}

- (void)drawRect:(NSRect)dirtyRect
{
    NSLog(@"%@", animator);
    NSGradient* gradient = [[NSGradient alloc] initWithColorsAndLocations:
                            STROKE_COLOR, 0.03,
                            [NSColor colorWithCalibratedRed: 0.837 green: 0.837 blue: 0.837 alpha: 1], 0.46,
                            BAR_COLOR, 0.49,
                            FILL_COLOR, 1.0, nil];

    CGFloat length = dirtyRect.size.width;
    length -= 3;

    NSBezierPath* roundedRectanglePath = [NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.5, 0.5, dirtyRect.size.width, 10) xRadius: 6.5 yRadius: 6.5];
    [[NSColor lightGrayColor] setFill];
    [roundedRectanglePath fill];

    if ([self isIndeterminate]) {
        indeterminate_X_1++;
        if (indeterminate_X_1 == dirtyRect.size.width) {
            indeterminate_X_1 = 0;
        }
        indeterminate_X_2++;
        if (indeterminate_X_1 == 14) {
            indeterminate_X_2 = 0;
        }
        indeterminate_X_3++;
        if (indeterminate_X_1 == 28) {
            indeterminate_X_3 = 0;
        }
        [self drawAnimationInRect:dirtyRect];
    } else
    {
        CGFloat width = floor(dirtyRect.size.width * [self doubleValue]/[self maxValue]);
        if (width >= 1) {
            NSRect fillRect = NSMakeRect(2, 2, floor(length * [self doubleValue]/[self maxValue]), 7);
            NSBezierPath* roundedRectangle2Path = [NSBezierPath bezierPathWithRoundedRect: fillRect xRadius: 6 yRadius: 6];
            [gradient drawInBezierPath: roundedRectangle2Path angle: 90];
        }
    }
}

- (void)drawAnimationInRect:(NSRect)rect
{
    NSColor* fillColor = [NSColor colorWithCalibratedRed: 1 green: 1 blue: 1 alpha: 1];
    NSColor* strokeColor = [NSColor colorWithCalibratedRed: 0.804 green: 0.804 blue: 0.804 alpha: 1];
    NSColor* barColor = [NSColor colorWithCalibratedRed: 0.87 green: 0.87 blue: 0.87 alpha: 1];
    NSGradient* gradient = [[NSGradient alloc] initWithColorsAndLocations:
                            strokeColor, 0.03,
                            [NSColor colorWithCalibratedRed: 0.837 green: 0.837 blue: 0.837 alpha: 1], 0.46,
                            barColor, 0.49,
                            fillColor, 1.0, nil];
    NSRect ovalRect = NSMakeRect(indeterminate_X_1, rect.origin.y + 1, 9, 9);
    NSBezierPath *oval = [NSBezierPath bezierPathWithOvalInRect:ovalRect];
    if (indeterminate_X_2 > 0) {
        NSRect ovalRect2 = NSMakeRect(indeterminate_X_2, rect.origin.y + 1, 9, 9);
        NSBezierPath *oval2 = [NSBezierPath bezierPathWithOvalInRect:ovalRect2];
        [gradient drawInBezierPath:oval2 angle:90];
    }
    if (indeterminate_X_3 > 0) {
        NSRect ovalRect3 = NSMakeRect(indeterminate_X_3, rect.origin.y + 1, 9, 9);
        NSBezierPath *oval3 = [NSBezierPath bezierPathWithOvalInRect:ovalRect3];
        [gradient drawInBezierPath:oval3 angle:90];
    }
    [gradient drawInBezierPath:oval angle:90];
}

@end

复制进度后,我将doubleValue设置为0并隐藏指示符。 我怎样才能阻止重绘?

Konobi

0 个答案:

没有答案