UIActivityIndi​​catorView不是动画,为什么?

时间:2014-08-02 15:06:57

标签: ios uiactivityindicatorview

我有一个UIActivityIndi​​catorView,我从Notification Center收到的通知开始和停止。虽然我收到了通知,但我通过“执行选择器OnMainThread”来调用“开始”和“停止”。它不会开始动画,也不会变得可见。

这是我的代码:

-(void)stopAnimation:(id)sender {
    if( _saveActivityIndicatorView.isAnimating ) {
        [_saveActivityIndicatorView stopAnimating];
    }
}

-(void)startAnimation:(id)sender {
    if( !_saveActivityIndicatorView.isAnimating ) {
        [_saveActivityIndicatorView startAnimating];
    }
}

-(void)saveStarted{
    NSLog(MRVaccinationEventsUpdateStarted);
    [self performSelectorOnMainThread:@selector(startAnimation:) withObject:self waitUntilDone:YES];
}

-(void)saveCompleted{
    NSLog(MRVaccinationEventsUpdateCompleted);
    [self performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES];
}

这就是我发布和观察通知的方式(两者都在同一个文件中):

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(saveStarted)
                                             name:MRVaccinationEventsUpdateStarted
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(saveCompleted)
                                             name:MRVaccinationEventsUpdateCompleted
                                           object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:MRVaccinationEventsUpdateStarted object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:MRVaccinationEventsUpdateCompleted object:nil];

请注意,我收到了通知。 这段代码有什么问题?

1 个答案:

答案 0 :(得分:0)

这是因为开始和停止动画处于同一循环中。

[[NSNotificationCenter defaultCenter] postNotificationName:MRVaccinationEventsUpdateStarted object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:MRVaccinationEventsUpdateCompleted object:nil];

您应该在一段时间后发布第二个通知,例如

///三秒

delayInSeconds = 3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [[NSNotificationCenter defaultCenter] postNotificationName:MRVaccinationEventsUpdateCompleted object:nil];
});