0

アプリケーションの読み込みアイコンを作成しました。アプリケーションがマップをロードしてマーカーを配置している間、画面にローディング アイコンが回転表示されます。私の現在のコードでは、読み込みアイコンが表示されますが、マーカーがすべてマップ上に配置され、すべての読み込みが終了したときにのみ回転します。私はすべてを試しましたが、誰か助けてくれますか?

以下にコードを添付します。これが最善の方法ではないことは理解しています。マップの読み込み時に回転させるのに何が間違っているのかを見つけたら、それを整理する予定です。

ありがとう。

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


        }
      }


}



 }
4

1 に答える 1

1

バックグラウンドで実行する必要があるコードの部分 (少なくともネットワーク リクエスト) と、メインで実行する必要があるコード (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
    });
});

そのため、構文に夢中にならないように、2 つのメソッドを作成します。最初のメソッドはネットワーク リクエストで使用する blogURL を生成し、2 つ目は NSData の結果を取得して他のすべてを行います。このように、その内部ディスパッチにネストされたワンライナー メソッド呼び出しだけがあります。

于 2015-12-29T01:59:34.830 に答える