Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Event Loop Question

coullscoulls Posts: 172Registered Users
edited July 2011 in iPhone SDK Development
Hi all,

OK - I have a loop that needs to repeatedly call setBackgroundColor on a UIImageView using a series of different periods. If you were to imaging a morse code flasher, you'd be along the same lines with the dots and dashes being different lengths, along with a predefined "gap" in the middle. The problem is the loop doesn't give the view time to actually process the call - so I'm scratching my head over which way to do this.

Two ways I've come up with is pre-scheduling an NSTimer to fire at all the future points, or to use an NSThread to fire over and over and then switch the view...

Both ways appear in my head to be a bit complicated - but bare in mind the 20 years of VB "training" I had before starting with Objective-C a few years back.

Cheers,

Jase
Post edited by coulls on

Replies

  • coullscoulls Posts: 172Registered Users
    edited July 2011
    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
  • Duncan CDuncan C Posts: 8,149Tutorial Authors, Registered Users
    edited July 2011
    coulls;358455 said:
    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:


    -(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]
    ];
    }
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • coullscoulls Posts: 172Registered Users
    edited July 2011
    Duncan C;358501 said:
    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.[/CODE]
    Doh! Now I feel like a twit... Will try again tonight and let you know how it pans out...

    Thanks!

    Jase
Sign In or Register to comment.