English 中文(简体)
A. 在一个方法内设置新南威尔士和新南威尔士大学
原标题:trouble setting NSWindow and UIView BackgroundColor in a loop within a method

I have great trouble setting background color for NSWindow (Mac OS X) and UIView (iOS) within a method or in a loop. Following code of setting background color only works for the last line of code in loop, which is when all values of i, j, k are 255, as screen turn to white as a proper mixture of RGB.

`- (void) ChangeColorLoop
{
for (int i=0; i<=255; i++)
    for (int j=0; j<=255; j++)
        for (int k=0; k<=255; k++)
        {
            [NSThread sleepForTimeInterval: 0.1];
            //NSLog(@"%d, %d, %d", i, j, k);
            float red = (float) i/255;
            float green = (float) j/255;
            float blue = (float) k/255;    
            float alpha = 1.0f;
            UIColor *mixedColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
            //BackgroundView.backgroundColor = mixedColor;
            [BackgroundView setBackgroundColor:mixedColor];
        }
}`

Thanks in advance,

Jerry

最佳回答

setBackgroundColor: information is only to be actioned when their Code re to the event loop, which it don t to the fact end. 因此,只有最后的颜色“摄入量”才能做到。

So, you need to run your loop on a background thread. But the setBackgroundColor: call needs to be on the main thread (since that s where all UI code goes).

If you use GCD, your logic needs to be something like:

-(void)changeColorLoop {
  dispatch_async(backgroundqueue, ^{
    for (;;) {
      // Do stuff
      dispatch_sync(main_queue, ^{
        [BackgroundView setBackgroundColor:mixedColor];
      });
    }
  });
}
问题回答

从你们的评论来看,这符合你们的意见。 我看到了几个问题。 你不看到肤色变化的根本原因是,你的 lo子正在阻挡主线,人们的看法(通常)只是在 run周期结束时重新提出。 直到你整个休息期结束为止,情况才发生。 解决这一问题有多种途径。 最简单的是,每度通过您的休息室进行手工操作。 您也可将这项工作转至背景,并发送<条码>查询:。 页: 1 这样做的另一个方式是利用一个时间者(无论是NStimer还是一个时间发送源)定期更新肤色。

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.

EDIT: Here s some sample code showing the timer approach I mentioned:

@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 >= 1.0) {
        [self.colorChangeTimer invalidate];
        self.colorChangeTimer = nil;
    }
    if (self.green > 1.0) {
        self.green = 0.0;
        self.red += 0.01;
    }
    if (self.blue >= 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




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签