2016-04-08 5 views
0

tamamlandığında görevin tamamlanmasından sonra bir iş parçacığı içinde yükleme animasyonu yükleme Bir yükleme animasyonunu görüntülemek için ayrı bir iş parçacığı basıldığında başlatılan bir düğmem var. Bunu yapmanın nedeni, düğmeye basılır basılmaz yükleme gif'i görüntülenir, diğer prosedür devam eder ve uyarı tamamlandığında tamamlanır. Elimdeki sorun, uyarı kapatıldıktan sonra animasyonu gizliyor.Başka bir

- (IBAction)buttonPressed:(id)sender { 
    [NSThread detachNewThreadSelector:@selector(loadAnimation) toTarget:self withObject:nil]; 

    ... do other things; 

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Complete" message:@"other things done" delegate: self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert setTag:1]; 
    [alert show]; 
} 

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == 1) { 
     loadingGif.hidden=YES; 
    } 
} 

yükleme gif:

- (void) loadAnimation { 
loadingGif.hidden=NO; 
NSArray *imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"0.gif"], [UIImage imageNamed:@"1.gif"], [UIImage imageNamed:@"2.gif"], [UIImage imageNamed:@"3.gif"], nil]; 

loadingGif = [[UIImageView alloc] initWithFrame:CGRectMake(487, 520, 50, 50)]; 
[self.view addSubview:loadingGif]; 
loadingGif.animationImages = imageArray; 
loadingGif.animationDuration = 1.5; 
[loadingGif startAnimating]; 
} 

animasyon yükleri ince ama uyarıdan Tamam tıklandığında kez durmaz. Animasyonu ana başlıktan diğerine başlatıldıktan sonra gizlemek mümkün mü?

cevap

1

Ana iş parçacığı dışındaki bir iş parçacığında UI değişiklikleri yapmanız gerektiğinden emin değilim.

- (IBAction)buttonPressed:(id)sender { 

    // load animation on the main thread 
    [self loadAnimation]; 

    // start a timer to do other stuff in 1 ms (will get executed on main thread) 
    [NSTimer scheduledTimerWithTimeInterval:0.001 
     target:self 
     selector:@selector(doOtherStuff) 
     userInfo:nil 
     repeats:NO]; 
} 

- (void)doOtherStuff { 

    ... do other things; 

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Complete" message:@"other things done" delegate: self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert setTag:1]; 
    [alert show]; 
1

Ben setHidden gibi çalışmıyor bu yüzden 'loadAnimation' veya 'UIAlertView' arka planda çalışırken düşünüyorum: alternatif bir yaklaşım kısa bir süre sonra yürütülecek hemen animasyonu göstermek ve diğer şeyler planlamak için NSTimer kullanmak olacaktır İstediğiniz.

- -

Neden ve

'ana iş parçacığı çalışan' 'iş bitene kadar bekleyin' emin olmak için böyle kodunuzu değişmez

I değişti Üzgünüm gereksiz arka plan bloğu kullandım. lütfen tekrar kontrol edin.

- (IBAction)buttonPressed:(id)sender 
{ 
    [self loadAnimation]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
    //  ... do other things 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Complete" message:@"other things done" delegate: self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alert setTag:1]; 
      [alert show]; 
     }); 
    }); 
} 

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 1) 
    { 
     loadingGif.hidden=YES; 
    } 
} 
+0

Cevabı takdir ediyorum. Bu benzer (uyarı olmadan) yükleme gif gizlemek gibi açıklar. Ben bir go.Thanks vereceğim – RGriffiths

+0

@RGriffiths Gereksiz blokları kaldırmak için kodumu değiştirdim. Bunun için üzgünüm. – negaipro