Quote:
Originally Posted by coulls
Update: So, I tried putting the task code into a dispatch queue. It ran identical to how it did in the loop, in that the screen still had no chance to catch up and process the setBackgroundColor messages...
*scratches head*
Will go back to the drawing board.
Cheers,
Coulls
|
You're not supposed to make UI changes except from the main thread. If you want to control everything from a worker thread, use performSelectorOnMainThread from the worker thread to get the main thread to update the UI.
Another way to do this without using threads is to use the method performSelector:withObject:afterDelay.
You could fire off the same method with different delays, and have the method change the color as needed.
The code below assumes you have an outlet theView to the view who's background color you want to change, an integer instance variable colorIndex, and another NSArray instance variable colorsArray that contains an array of UIColor objects:
Code:
-(void) nextColor
{
theView.backgroundColor = [colorsArray objectAtIndex: colorIndex];
colorIndex++;
}
-(void) beginFlashing;
{
colorIndex = 0;
int index;
int count = 10;
//Create a C array of different delays for the different steps.
//We could also use an NSArray of NSNumbers. I was just lazy.
float[] delays = {0.2, 0.3, 0.5, 0.9, 1.0, 1.5, 1.9, 2.2, 2.7, 3.0}
for (index = 0; index < count; index++)
{
[self performSelector: @selector(nextColor)
withObject: nil
afterDelay: delays[index]
];
}