UIActivityIndi​​catorView不显示/开始animation

我有一个UIActivityIndi​​catorView的视图控制器和触发同步的button。 同步在一个单独的类中执行,通常在后台执行。 同步可以从我的应用程序的不同部分触发,我的视图控制器被通知同步事件。

现在,当我点击button时,UIActivityIndi​​catorView不会变得可见或不生动。

这是我在viewDiDLoad中的代码:

self.syncNowActivityIndicatorView.hidesWhenStopped = YES; self.syncNowActivityIndicatorView.color = [MRStyleController getFirstLightColor]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leechStarted) name:MRLeechStartedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeStarted) name:MRMergeStartedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileSyncStarted) name:MRFileSyncStartedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leechFailed) name:MRLeechFailedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeFailed) name:MRMergeFailedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileSyncFailed) name:MRFileSyncFailedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leechCompleted) name:MRLeechCompletedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeCompleted) name:MRMergeCompletedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileSyncCompleted) name:MRFileSyncCompletedNotification object:nil]; 

我已经读过其他一些post,如果我用相同的方法启动和停止指示器视图,它将无法正常工作,因为在执行方法时ui不会更新。 所以,animation必须在一个单独的线程中启动和停止,但在我的情况下,启动和停止调用都是用不同的方法。

请注意,我正在收到我的日志显示的通知。

以下是其他方法:

 -(void)leechStarted{ NSLog(@"Leech started"); if (!_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView startAnimating]; } } -(void)mergeStarted{ NSLog(@"Merge started"); if (!_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView startAnimating]; } } -(void)fileSyncStarted{ NSLog(@"FileSync started"); if (!_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView startAnimating]; } } -(void)leechFailed{ NSLog(@"Leech failed"); if (_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView stopAnimating]; } } -(void)mergeFailed{ NSLog(@"Merge failed"); if (_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView stopAnimating]; } } -(void)fileSyncFailed{ NSLog(@"FileSync failed"); if (_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView stopAnimating]; } } -(void)leechCompleted{ NSLog(@"Leech completed"); if (_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView stopAnimating]; } } -(void)mergeCompleted{ NSLog(@"Merge completed"); if (_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView stopAnimating]; } } -(void)fileSyncCompleted{ NSLog(@"FileSync completed"); if (_syncNowActivityIndicatorView.isAnimating) { [_syncNowActivityIndicatorView stopAnimating]; } } 

在这里button的IBAction:

 - (IBAction)syncNow:(id)sender { // perform synchronisation CoreDataHelper *cdh = [(MRMedSafeAppDelegate *) [[UIApplication sharedApplication] delegate] cdh]; if ([cdh canSynchronize]) { [cdh mergeEnsemble]; [cdh synchroniseFiles]; } } 

mergeEnsemble发布mergeEnsemble和合并通知,以及synchroniseFiles文件同步通知,并根据日志收到它们。

您必须在主线程上执行启动/停止animation:

 -(void)stopAnimation:(id)sender { if( _syncNowActivityIndicatorView.isAnimating ) { [_syncNowActivityIndicatorView stopAnimating]; } } -(void)startAnimation:(id)sender { if( !_syncNowActivityIndicatorView.isAnimating ) { [_syncNowActivityIndicatorView startAnimating]; } } -(void)leechStarted{ NSLog(@"Leech started"); [self performSelectorOnMainThread:@selector(startAnimation:) withObject:self waitUntilDone:YES]; } -(void)leechFailed{ NSLog(@"Leech failed"); [self performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES]; }