Objective C 在计时器上加载图标

Objective C Loading Icon on Timer

我已经为我的应用程序创建了一个加载图标。当应用程序加载地图并放置标记时,我有一个加载图标显示在屏幕上旋转。使用我当前的代码,加载图标会显示,但仅当标记全部放置在地图上并且所有内容都已完成加载时才会旋转。我已经尝试了一切,有人可以帮忙吗?

我将在下面附上代码,我知道这不是最好的方法,我打算在我发现我做错了什么让它在加载地图时旋转时整理它。

谢谢。

- (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) {
            }
        }];
    }
}

已编辑:在下面添加了地图代码

-(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);


        }
      }


}



 }

我们需要梳理代码中应该在后台完成的部分——至少是网络请求——以及必须在主程序上完成的代码——任何改变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
    });
});

因此,您不会被语法逼疯,构建两个方法,第一个生成用于网络请求的 blogURL,第二个获取 NSData 结果并执行其他所有操作。这样,只有一个 one-liner 方法调用嵌套在该内部调度中。