活动指示器停止动画无效

时间:2014-11-26 12:43:43

标签: objective-c io nsnotificationcenter uiactivityindicatorview

所以基本上我有一个活动指示器,我在发送异步请求之前就这样开始了

[self.activityIndicator setBounds:self.view.frame];
[self.activityIndicator setCenter:self.view.center];
[self.activityIndicator setAlpha:1.0f];
UIColor *activityBackgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3];
[self.activityIndicator setBackgroundColor:activityBackgroundColor];
[self.activityIndicator startAnimating];

[self sentRequestToFacebook];
[calender returnCalenderEvents];

异步请求的完成处理程序使用NSNotification通知主对象对象来停止我喜欢的活动指示

if ([[notification name]isEqualToString:@"calenderData"]) {
    calenderDataReceived = YES;
    NSDictionary* userInfo = notification.userInfo;
    calendarData = [userInfo objectForKey:@"data"];
}
else if ([[notification name]isEqualToString:@"facebookData"])
{
    facebookDataReceived = YES;
    NSDictionary* userInfo = notification.userInfo;
    facebookData = [userInfo objectForKey:@"data"];
}

if (facebookDataReceived&&calenderDataReceived) {
    facebookDataReceived = NO;
    calenderDataReceived = NO;
    //Now do the mergings
    self.activityIndicator.hidden = YES;
    [self.activityIndicator stopAnimating];
    [self.activityIndicator removeFromSuperview];
    NSLog(@"%@",calendarData);
    NSLog(@"%@",facebookData);
}

但是,此代码没有达到预期的效果,并且在停止之前运行此代码后,活动指示器仍会显示几秒钟的动画。

这里有什么问题

1 个答案:

答案 0 :(得分:3)

如果指示灯在几秒钟后停止,请确保停止指示器的代码在主线程上运行。如果它在不同的线程上运行,那么使用这个片段:

dispatch_async(dispatch_get_main_queue(), ^{
    self.activityIndicator.hidden = YES;
    [self.activityIndicator stopAnimating];
    [self.activityIndicator removeFromSuperview];
})