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 8557103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:29:58+00:00 2026-06-11T15:29:58+00:00

I am trying to implement city lights animations by having an array of CGRect’s

  • 0

I am trying to implement city lights animations by having an array of CGRect’s with the coordinates of each light. Then creating UIViews around these CGRects. This logic (Thanks to Darren for helping with this logic) is working fine however after the animation is complete, all other elements (UIButtons, Sliders, Other UIImageViews etc) on screen become inaccessible. Even my Swipe gestures are not responding. Before and during the animation, all elements respond well BUT once the animation is complete, they all become inaccessible. I also tried [UIView bringSubviewToFront:] to bring some of the elements in front to see if that helps to make them accessible but it didn’t help. I think that’s not the issue because even if I try to create the lights view by sending them to background [self.view sendSubviewToBack:light]; everything becomes inaccessible after the animation is complete.

I would appreciate if someone can help/suggest me what am I missing.

Here is my code logic and corresponding scene for city lights animation. rects & lits are ivars.

enter image description here

- (void)viewDidLoad {
    pageCount=5;
    AVAudioSession * audioSession = [AVAudioSession sharedInstance];
    //Setup the audio session
    //...
    pageNum=1;
    //put imageviews in place
    //...
    //load a page
    [self loadPage];
    [self loadAudio];

    [self cityLightsSetUp];
    [super viewDidLoad];
}


-(void)loadPage{
    //Logic to load page...    
    /...
    if (pageNum == 3){
      [self startCityLights];
    }
}


- (void)cityLightsSetUp
{
    rects = [[NSMutableArray alloc] init];
    lit = [[NSMutableArray alloc] init];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 217, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 217, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(84, 217, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(97, 217, 10, 10)]];

    [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 231, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 231, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(84, 231, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(97, 231, 10, 10)]];

    [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 245, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 245, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(84, 245, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(97, 245, 10, 10)]];

    [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 258, 10, 10)]];
    [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 258, 10, 10)]];
}

- (void)startCityLights
{
    [self lightRandomLight];
    [self performSelector:@selector(switchOffRandomLight) withObject:nil afterDelay:1.0];
}

- (void)lightRandomLight
{
    BOOL escape = NO;
    int rand;

    while (!escape) {
        BOOL alreadyLit = NO;
        rand = arc4random() % [rects count];
        // Check if already lit
        for (UIView *view in lit) {
            CGRect litRect = view.frame;
            CGRect ranRect = [[rects objectAtIndex:rand] CGRectValue];
            if (CGRectContainsRect(litRect, ranRect)) {
                alreadyLit = YES;
            }
        }

        if (!alreadyLit) {
            UIView *light = [[UIView alloc] initWithFrame:[[rects objectAtIndex:rand] CGRectValue]];
            light.backgroundColor = [UIColor orangeColor];
            [lit addObject:light];
            [self.view addSubview:light];
            //[self.view sendSubviewToBack:light];
            escape = YES;
        }
    }

    [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2];
}

- (void)switchOffRandomLight
{
     NSLog(@"switchOffRandomLight");
    int rand = arc4random() % [lit count];
    UIView *light = [lit objectAtIndex:rand];
    [lit removeObject:light];
    [light removeFromSuperview];

    [self performSelector:@selector(switchOffRandomLight) withObject:nil afterDelay:0.5];
}
  • 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. Editorial Team
    Editorial Team
    2026-06-11T15:29:59+00:00Added an answer on June 11, 2026 at 3:29 pm

    It appears that once all lights are lit, the while loop runs continuously trying to get a random number that isn’t lit. This is what will be blocking the main thread.

    All you need is to check if all lights are lit at the beginning of the method and continue if not.

    At the bottom of your lightRandomLight method, replace

    [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2];
    

    with

    if ([lit count] != [rects count]) {
        [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2];
    } else {
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
    }
    

    and it’ll stop when all the lights are lit.

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

Sidebar

Related Questions

Trying to implement a search similar to here .This searches properties based on city,locality,property
I'm trying to implement a link list which stores the city name (though you
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
I am trying implement a most recent widget into my tumblr post. So far,
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
Trying to implement google C2DM service. registrationIntent.putExtra(app, PendingIntent.getBroadcast(context,0,new Intent(), 0)); registrationIntent.putExtra(sender,example@gmail.com); context.startService(registrationIntent); Almost every
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
Trying to implement a rating system of users and postings. What is the best
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
Im trying to implement a destructor for the objects of linked-list Iv created. I

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.