首页 文章

iOS:如何正确停止活动指标?

提问于
浏览
3

我想在默认NSNotificationCenter调用的方法中使用postNotificationName停止指示器的动画 . 所以我在主线程上做这个

-(void)method
{
    ...
    [ind performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

它确实无效 . 方法被正确调用,任何其他被调用的选择器完成它们的工作但不是stopAnimating . 我把[ind stopAnimating]放在另一个函数中,然后通过performSelectorOnMainThread调用它,但它仍然无效 .

3 回答

  • 2

    您还可以使用以下方法在单个方法中启动和停止主线程上的活动指示器,还可以异步执行代码 -

    - (void)showIndicatorAndStartWork
    {
        // start the activity indicator (you are now on the main queue)
        [activityIndicator startAnimating];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // do your background code here
    
            dispatch_sync(dispatch_get_main_queue(), ^{
                // stop the activity indicator (you are now on the main queue again)  
            [activityIndicator stopAnimating];
            });
        });
    }
    
  • 5

    试试这个...

    创建一个停止动画的方法

    -(void)stopAnimationForActivityIndicator
    {
        [ind stopAnimating];
    }
    

    像这样替换你的方法 -

    -(void)method
    {
        ...
        [self performSelectorOnMainThread:@selector(stopAnimationForActivityIndicator) withObject:nil waitUntilDone:NO];
    }
    

    应该做的神奇......

  • 3

    试试:

    -(void)method
    {
        dispatch_async(dispatch_get_main_queue(), ^{ 
           [ind stopAnimating];   
        });
    }
    

相关问题