0

I have created a loading icon for my application. While the application is loading the map and placing the markers I have a loading icon displaying on the screen rotating. With my current code the loading icon shows, but only rotates when the markers are all placed on the map and everything is finished loading. I have tried about everything, can anyone help?

I will attach code below, I do understand this is not the best way to do it and I plan to neaten it up once I find out what I am doing wrong getting it to rotate when the map is loading.

Thanks.

- (void)viewDidLoad
{
    [super viewDidLoad];
    loading=@"0";

    rotateTimer = [NSTimer scheduledTimerWithTimeInterval:0.3
                                                   target:self
                                                 selector:@selector(rotateMove)
                                                 userInfo:nil
                                                  repeats:YES];
}

-(void)rotateMove
{
    if([loading  isEqual:@"1"])
    {
        [rotateTimer invalidate];
        rotateTimer = nil;
    }
    if([loading  isEqual:@"0"])
    {
        NSLog(@"move");
        [UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            [self.rotate setTransform:CGAffineTransformRotate(self.rotate.transform, M_PI_2)];
        } completion:^(BOOL finished){
            if (finished) {
            }
        }];
    }
}

EDITED: ADDED MAP CODE BELOW

-(void)mapload
{

 [self.mapView clear];


NSString *lat = [[NSString alloc] initWithFormat:@"%g", latitude];
NSString *lng = [[NSString alloc] initWithFormat:@"%g", longitude];
NSURL *blogURL = 
NSLog(@"URL = %@", blogURL);
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
if(jsonData == nil)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Please check your internet connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    return;
}
else{



    NSError *error = nil;
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSArray *test = [dataDictionary objectForKey:@"test"];

    self.bottombutton.hidden=FALSE;
    self.time.hidden=FALSE;
    self.mins.hidden=FALSE;
   self.rotate.hidden=TRUE;
    int tes = [[test[0] valueForKey:@"time"] intValue];


    }

    for (int i = 0; i < [test count]; i++)    {
        for(NSDictionary *coordinates in test){

            double la=[coordinates[@"lat"] doubleValue];
            double lo=[coordinates[@"long"] doubleValue];

            CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
            CLLocationCoordinate2D coordi=loca.coordinate;

            GMSMarker *marker= [[GMSMarker alloc] init];
            marker=[GMSMarker markerWithPosition:coordi];
            marker.snippet = coordinates[@"name"];
            marker.map = self.mapView;
            marker.appearAnimation = kGMSMarkerAnimationPop;

            UIImage * image = [UIImage imageNamed:@"mapiconlarge"];
            CGSize sacleSize = CGSizeMake(45, 45);
            UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
            [image drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
            UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            NSLog(@"markeradded");
            marker.icon = resizedImage;
            NSLog(loading);


        }
      }


}



 }
Curtis Boylan
  • 827
  • 1
  • 7
  • 23
  • try loading the timer on viewdidappear – Teja Nandamuri Dec 29 '15 at 01:20
  • same issue, just tried that. – Curtis Boylan Dec 29 '15 at 01:26
  • Are you getting the @"move" log output? What you're describing would occur if the map init was blocking the main thread. Another symptom of that would be that you wouldn't see any "move" logging until the end. – danh Dec 29 '15 at 01:29
  • the method to load the map seems to freeze the rotate method. I placed a test NSLog in both and when the map method is doing its stuff, the rotate method won't spit out debug text until the map load method is completed although the timer started before the map load method. – Curtis Boylan Dec 29 '15 at 01:29
  • i'm not getting the move log output until the map is finished loading. – Curtis Boylan Dec 29 '15 at 01:30
  • Yes. That's the problem then. Please post the map loading code. Probably can be fixed quickly by running from within a dispatch_asynch – danh Dec 29 '15 at 01:30
  • I will post it above now. I have tried using that but for some reason a lot of things such as hiding buttons didn't work, Posting code now. – Curtis Boylan Dec 29 '15 at 01:32
  • Posted that above now, I removed some non major code which was calculating time on the map which is not part of the main function. Left the main map view in, it gets the map locations from a sever. – Curtis Boylan Dec 29 '15 at 01:37

1 Answers1

1

We need to tease apart the part of your code that should be done in the background -- at the very least, the network request -- and the code that must be done on the main -- anything that changes the UI...

// here, everything you do to prepare for the network request

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // make the network request
    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
    dispatch_async(dispatch_get_main_queue(), ^{
        // here everything you do after with the json data result
    });
});

So you don't drive yourself crazy with syntax, build two methods, the first one produces blogURL for use on the network request, the second, takes the NSData result and does everything else. This way, there's just a one-liner method invocation nested in that inner dispatch.

danh
  • 62,181
  • 10
  • 95
  • 136
  • Will hiding buttons work in this method? I've tried a similar one before and the hiding and unhiding of my views and buttons didn't work. I will try it when I get home and let you know what happens. – Curtis Boylan Dec 29 '15 at 11:00
  • I don't see why not try setting alpha == 0.0. It's animatable too. – danh Dec 29 '15 at 15:44
  • Just got home and tried the answer you gave, its fully working and the hiding of my buttons is working too. Thank you so much! – Curtis Boylan Dec 29 '15 at 19:34