Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds from your comment like this is running on the main thread. I see several problems with it. The fundamental reason why you're not seeing the color change is because your loop is blocking the main thread, and views are (normally) only redrawn at the end of a run loop cycle. That doesn't happen until after your entire loop has finished. There are various ways to solve this. The simplest is probably to manually run the run loop each time through your for loop. You could also move this work to a background thread and dispatch the <code>setBackgroundColor:</code> call to the main queue. Another way to do this would be to use a timer (either NSTimer or a timer dispatch source) to periodically update the color.</p> <p>In any case, you shouldn't be sleeping the main thread in the middle of your for loop. There's almost never a good reason to sleep the main thread, and doing so blocks your UI from updating or processing user input events.</p> <p>EDIT: Here's some sample code showing the timer approach I mentioned:</p> <pre><code>@interface AppDelegate () @property (nonatomic, strong) NSTimer *colorChangeTimer; @property (nonatomic) float red; @property (nonatomic) float green; @property (nonatomic) float blue; @end @implementation AppDelegate - (id)init { self = [super init]; if (self) { self.red = 0.5; self.blue = 0.5; self.green = 0.5; } return self; } - (void)updateColors:(NSTimer *)timer { NSColor *newColor = [NSColor colorWithDeviceRed:self.red green:self.green blue:self.blue alpha:1.0]; [self.window setBackgroundColor:newColor]; if (self.red &gt;= 1.0) { [self.colorChangeTimer invalidate]; self.colorChangeTimer = nil; } if (self.green &gt; 1.0) { self.green = 0.0; self.red += 0.01; } if (self.blue &gt;= 1.0) { self.blue = 0.0; self.green += 0.01; } else { self.blue += 0.01; } } - (IBAction)changeColors:(id)sender { if (self.colorChangeTimer != nil) return; self.colorChangeTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateColors:) userInfo:nil repeats:YES]; } @synthesize window = _window; @synthesize colorChangeTimer = _colorChangeTimer; @synthesize red = _red; @synthesize green = _green; @synthesize blue = _blue; @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload