NSView в какао отказывается перерисовывать, что бы я ни пытался - PullRequest
0 голосов
/ 15 февраля 2019

Я пишу приложение для Mac, чтобы измерить цвета на экране.Для этого у меня есть ColorView, который заполняет безьерус определенным цветом.При измерении он подходит для большинства исправлений, но в данный момент перерисовка колеблется, и представление больше не обновляется.Я долго искал, перепробовал много предложенных решений.Ни один из них не является водонепроницаемым.

Мой фактический код:

    [colorView setColor:[colorsForMeasuring objectAtIndex:index]];
    [colorView setNeedsDisplay:YES];
    [colorView setNeedsLayout:YES];
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(mainQueue,^{
        [colorView setNeedsDisplay:YES];
        [colorView setNeedsLayout:YES];
        [colorView updateLayer];
        [colorView displayIfNeeded];
        [colorView display];
    });
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    [NSApp runModalSession:aSession];

Код отрисовки моего colorView выглядит следующим образом:

- (void)display
{
    CALayer *layer = self.layer;
    [layer setNeedsDisplay];
    [layer displayIfNeeded];
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    [self internalDrawWithRect:self.bounds];
}
- (void)internalDrawWithRect:(CGRect)rect
{
    NSRect bounds = [self bounds];
    [color set];
    [NSBezierPath fillRect:bounds];
}
 - (void)drawRect:(CGRect)rect {
    [self internalDrawWithRect:rect];
}

Любая помощь будет приветствоваться!Мой оригинальный код был намного проще, но я продолжал добавлять вещи, которые могли бы помочь.

1 Ответ

0 голосов
/ 18 февраля 2019

Как я уже говорил, исходный код был довольно простым и простым: измерительный цикл:

for ( uint32 index = 0; index < numberOfColors && !stopMeasuring; index++ )
{
    #ifndef NDEBUG
    NSLog( @"Color on screen:%@", [colorsForMeasuring objectAtIndex:index] );
    #endif

    [colorView setColor:[colorsForMeasuring objectAtIndex:index]];
    [colorView setNeedsDisplay:YES];
    [colorView displayIfNeeded];

    // Measure the displayed color in XYZ values
    CGFloat myRed, myGreen, myBlue, myAlpha;
    [[colorsForMeasuring objectAtIndex:index] getRed:&myRed green:&myGreen blue:&myBlue alpha:&myAlpha];
    float theR = (float) myRed;
    float theG = (float) myGreen;
    float theB = (float) myBlue;
    xyzData = [measDeviceController measureOnceXYZforRed:255.0f*theR Green:255.0f*theG Blue:255.0f*theB];
    if ( [xyzData count] > 0 )
    {
        measuringResults[index*3]           = [[xyzData objectAtIndex:0] floatValue];
        measuringResults[(index*3) + 1] = [[xyzData objectAtIndex:1] floatValue];
        measuringResults[(index*3) + 2] = [[xyzData objectAtIndex:2] floatValue];
        #ifndef NDEBUG
        printf("Measured value X=%f, Y=%f, Z=%f\n", measuringResults[index*3], measuringResults[(index*3) + 1], measuringResults[(index*3) + 2]);
        #endif
    } }

Чертеж MCTD_ColorView, который просто наследуется от NSView:

- (void)setColor:(NSColor *)aColor;
{
    [aColor retain]; 
    [color release]; 
    color = aColor;
}

- (void)drawRect:(NSRect)rect
{
    NSRect bounds = [self bounds];
    [color set];
    [NSBezierPath fillRect:bounds];
}
- (void)dealloc
{
    [color release];
    [super dealloc];
}

При запуске моего цикла измерения и после помещения точек останова в setColor и drawRect отладка всегда останавливается в setColor, но никогда в drawRect.Без отладки вид остается белым, а я вижу все разные цвета.

Как видно из моего первого поста, я много чего пробовал, чтобы нарисовать его, но все они провалились.

...