Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-21-2011, 07:10 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: I'm a Brit-pat.
Posts: 171
coulls is on a distinguished road
Default Event Loop Question

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
coulls is offline   Reply With Quote
Old 07-25-2011, 01:16 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: I'm a Brit-pat.
Posts: 171
coulls is on a distinguished road
Default

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
coulls is offline   Reply With Quote
Old 07-25-2011, 03:28 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by coulls View Post
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]
    ];
  }
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-26-2011, 07:20 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: I'm a Brit-pat.
Posts: 171
coulls is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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
coulls is offline   Reply With Quote
Reply

Bookmarks

Tags
event loop, looping, setbackgroundcolor, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 408
16 members and 392 guests
7twenty7, blasterbr, buggen, chiataytuday, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, n00b, nyoe, pungs, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:57 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0