当前位置:  开发笔记 > 编程语言 > 正文

显示UIActivityIndi​​cator持续x秒

如何解决《显示UIActivityIndi​​cator持续x秒》经验,为你挑选了1个好方法。

我有一个UITableViewController,它是RootViewController.它的XIB只有一个表格视图.我通过IB添加了一个UIActivityIndi​​cator并创建了一个IBOutlet.我已经以编程方式向RootViewController添加了一个带有删除按钮的工具栏.当用户单击工具栏的删除按钮时,我想以屏幕为中心显示指示符.下面的方法从数据库中删除数据,这种情况发生得非常快.我想让用户知道正在发生的事情.如果我在没有sleep()的情况下运行下面的代码,它会快速发生并且我看不到指示器.但有些人出于某种原因,我还是没有看到这个指标.我猜睡眠()块重新粉刷?如果我拿出睡眠()和最后两行,我会看到指示器但当然它永远不会消失.它还显示在窗口的左上角.

如何让下面的代码工作,使指示器至少显示1/2 - 1秒?

如何将指示器对准窗口中间?

[self.navigationController.view addSubview:activityIndicator];
[activityIndicator startAnimating];
[activityIndicator setNeedsDisplay];

//do something which might happen really fast

sleep(1); //create illusion
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];

Brent Royal-.. 6

在处理完事件并返回到运行循环之前,微调器不会开始旋转.解决这个问题的方法是让运行循环为你完成任务.

这是我的意思的一个例子:

- (IBAction)deleteAction:(id)sender {
    [self.navigationController.view addSubview:activityIndicator];
    [activityIndicator startAnimating];

    // Spinner won't start spinning until we finish processing this event, so
    // we're just going to schedule the rest of what we need to do.

    // doDelete: will run when the main thread gets its next event.
    [self performSelectorOnMainThread:@selector(doDelete:)
                           withObject:record
                        waitUntilDone:NO];

    // removeSpinner: will run in at least one second, but will wait if
    // another event (like the doDelete: one) is in the middle of running.
    [self performSelector:@selector(removeSpinner:)
               withObject:activityIndicator
               afterDelay:1.0];
}
- (void)doDelete:(id)record {
    [record delete];   // or whatever it is you need to do
}
- (void)removeSpinner:(UIActivityIndicator*)activityIndicator {
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
}

注意:此代码中没有任何内容可以保证在"休眠"期间阻止其处理其他触摸,因此请确保以某种方式锁定其他事件.



1> Brent Royal-..:

在处理完事件并返回到运行循环之前,微调器不会开始旋转.解决这个问题的方法是让运行循环为你完成任务.

这是我的意思的一个例子:

- (IBAction)deleteAction:(id)sender {
    [self.navigationController.view addSubview:activityIndicator];
    [activityIndicator startAnimating];

    // Spinner won't start spinning until we finish processing this event, so
    // we're just going to schedule the rest of what we need to do.

    // doDelete: will run when the main thread gets its next event.
    [self performSelectorOnMainThread:@selector(doDelete:)
                           withObject:record
                        waitUntilDone:NO];

    // removeSpinner: will run in at least one second, but will wait if
    // another event (like the doDelete: one) is in the middle of running.
    [self performSelector:@selector(removeSpinner:)
               withObject:activityIndicator
               afterDelay:1.0];
}
- (void)doDelete:(id)record {
    [record delete];   // or whatever it is you need to do
}
- (void)removeSpinner:(UIActivityIndicator*)activityIndicator {
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
}

注意:此代码中没有任何内容可以保证在"休眠"期间阻止其处理其他触摸,因此请确保以某种方式锁定其他事件.

推荐阅读
mobiledu2402851173
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有