Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 133709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:28:54+00:00 2026-05-11T06:28:54+00:00

I have a NSArray of UIImageViews that I want to loop over and quickly

  • 0

I have a NSArray of UIImageViews that I want to loop over and quickly swap out an ‘on’ and ‘off’ state. I wrote the code to do so in a for loop instead a method that was called when the user tapped a UIButton ( the button’s action ).

Here’s that loop:

for(int i = 0; i < [Images count]; i++) {     if( i > 0 ){         [self toggleImageViewOff:[Images objectAtIndex:i - 1]];     }      [self toggleImageViewOn:[Images objectAtIndex:i]];      [NSThread sleepForTimeInterval:0.5f]; } 

The UI did not update as I expected as I only ever saw the last UIImageView in the ‘on’ state. I figured that the drawing update of the views must occur in the main thread this code was also executing in. So I learned about performSelectorInBackground:withObject: . Performing the toggleImageViewOn/Off methods using this made the loop work. The problem is if I make the sleep interval too short I can have an ‘on’ update after an ‘off’ with Threads operating out of order.

So I had the bright idea of moving the whole loop with the sleep into its own method and calling that from the action method using performSelectorInBackground:withObject: . I tried that and I’m back to not getting an updated view until the loop is over.

That’s a long winded way to get to my question:

What’s the best way to animate this to guarantee the on/off code fires in the right order and still get view updates, even at high speeds? ( i.e. looping very quickly )

I tried to think about how I’d do it with CoreAnimation, but I can’t seem to get my head around how to do it there.

For bonus, here are the toggle methods:

- (void)toggleImageViewOn:(UIImageView *)theImageView {     [theImageView setImage:[UIImage imageNamed:@'on.png']]; }  - (void)toggleImageViewOff:(UIImageView *)theImageView {     [theImageView setImage:[UIImage imageNamed:@'off.png']]; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:28:55+00:00Added an answer on May 11, 2026 at 6:28 am

    You’re on the right track with moving the loop to a background thread, but you also need to make sure that you give the main run loop a chance to update the UI. You should be able to replace the direct calls to toggleImageViewOn: and toggleImageViewOff: with something like

    [self performSelectorOnMainThread:@selector(toggleImageViewOn:) withObject:[Images objectAtIndex:i] waitUntilDone:NO]; 

    This will do the UI update on the main thread, and by not waiting until the update is done you give the main run loop a chance to reach its end. You run into the same issue with things like progress bars, where they won’t change until the loop ends unless you do your updates from a background thread with a UI update call like the one above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your character pointers are unitialized, and point to a random… May 12, 2026 at 12:56 am
  • Editorial Team
    Editorial Team added an answer I wrote a fairly long answer to similar problem in… May 12, 2026 at 12:56 am
  • Editorial Team
    Editorial Team added an answer The subdialog element can be used to transition to forms… May 12, 2026 at 12:56 am

Related Questions

I'm trying to build a NSArray of methods in Objective-C . (What I'm trying
I have a server-side php-script that fetches results from a MySQL table. I am
Let's say I have an NSArray of NSDictionaries that is 10 elements long. I
How can I get a list (in the form of an NSArray or NSDictionary

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.