Сделать заставку из MacOS заставкой черного цвета? - PullRequest
1 голос
/ 19 января 2020

Я новичок в Obj- C, поэтому извиняюсь, если мой код запутан.

Я сделал заставку для DVD lo go, которая меняет цвет при каждом отскоке. Моя единственная проблема - это фон - он темно-серый, а не черный. Я надеюсь, что кто-нибудь может мне помочь.

Вот часть моего кода:

NSString * dvdPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"dvdlogo" ofType:@"png"];

self.dvdLogo = [[NSImage alloc] initWithContentsOfFile:dvdPath];[self hitWall];}return self;}

- (void)startAnimation
{[super startAnimation];}

- (void)stopAnimation
{[super stopAnimation];}

- (void)drawRect:(NSRect)rectParam
{const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
NSRect rect;
  rect.size = NSMakeSize(self.dvdWidth, self.dvdHeight);
    self.x += self.xSpeed;
    self.y += self.ySpeed;
    rect.origin = CGPointMake(self.x, self.y);
    self.dirtyRect = rect;

    [self.dvdLogo drawInRect:rect];

    CGPoint centre = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));

    if (centre.x + self.dvdWidth / 2 >= WIDTH || centre.x - self.dvdWidth / 2 <= 0) {
        self.xSpeed *= -1;
        [self hitWall];}

    if (centre.y + self.dvdHeight / 2 >= HEIGHT || centre.y - self.dvdHeight / 2 <= 0) {
        self.ySpeed *= -1;
        [self hitWall];}}

- (void)hitWall
{NSArray * colours = @[[NSColor redColor],
                      [NSColor blueColor],
                      [NSColor yellowColor],
                      [NSColor cyanColor],
                      [NSColor orangeColor],
                      [NSColor magentaColor],
                      [NSColor greenColor]];
    self.dvdColor = colours[arc4random() % [colours count]];
    [self.dvdLogo lockFocus];
    [self.dvdColor set];
    NSRect imageRect = {NSZeroPoint, [self.dvdLogo size]};
    NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
    [self.dvdLogo unlockFocus];}

- (void)animateOneFrame
{//[self setNeedsDisplay:true];
    [self setNeedsDisplayInRect:self.dirtyRect];
    return;}

Вот загрузка заставки , если вы хотите увидеть, что Я говорю о.

1 Ответ

1 голос
/ 19 января 2020

Если я следую вашему фрагменту кода и правильно выполнил задачу, ваш drawRect начинается с:

const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);

Это заполняет область, определенную rectParam, темно-серым. Почему вы выбрали 15.0f/255.0f для компонентов RGB? Черных просто 0, но их легче получить с помощью blackColor:

[[NSColor blackColor] setFill];

HTH

...