gpt4 book ai didi

ios - UIActivityIndi​​catorView 不显示/开始动画

转载 作者:行者123 更新时间:2023-11-29 02:47:25 25 4
gpt4 key购买 nike

我有一个带有 UIActivityIndi​​catorView 的 View Controller 和一个触发同步的按钮。同步在单独的类中执行,通常在后台执行。可以从我的应用程序的不同部分触发同步,并且我的 View Controller 会收到同步事件的通知。

现在,当我单击按钮时,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];

我在其他一些帖子中读到,如果我在同一个方法中启动和停止指标 View ,它将不起作用,因为在方法执行时 ui 没有更新。因此,动画必须在单独的线程中启动和停止,但在我的例子中,启动和停止调用都是在不同的方法中进行的。

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

其他方法如下:

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

这里是按钮的 IBAction:

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

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

最佳答案

您必须在主线程上执行开始/停止动画:

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

关于ios - UIActivityIndi​​catorView 不显示/开始动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24944793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com