为什么我的 UIActivityIndi​​catorView 不会停止动画?

发布于 2024-08-19 13:12:43 字数 956 浏览 4 评论 0原文

我正在尝试从我的应用程序委托管理活动指示器,这样我的任何视图都可以放置指示器。因此,我将其作为子视图添加到“窗口”并启动/停止,如下所示:

- (void)didStartActivity
{
    if( activityIndicator == nil ) {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicator.hidesWhenStopped = YES;
        activityIndicator.center = window.center;
        activityIndicator.transform = CGAffineTransformScale(CGAffineTransformIdentity, 6.0, 6.0);
    }
    NSLog(@"%s: starting the activityIndicator", __FUNCTION__);
    [window addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

我看到日志消息,所以我知道代码正在被调用。指示器位于中心,大小是默认大小的 6 倍。但是,stopAnimating 并没有停止。我唯一可以得出的结论是它需要在当前的视图控制器中运行。

- (void)didStopActivity
{
    NSLog(@"%s: stopping the activityIndicator", __FUNCTION__);
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
}

I am trying to manage the activity indicator from my App Delegate, that way any of my views can put the indicator up. So, I am adding it as a subview to 'window' and start/stop as follows:

- (void)didStartActivity
{
    if( activityIndicator == nil ) {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicator.hidesWhenStopped = YES;
        activityIndicator.center = window.center;
        activityIndicator.transform = CGAffineTransformScale(CGAffineTransformIdentity, 6.0, 6.0);
    }
    NSLog(@"%s: starting the activityIndicator", __FUNCTION__);
    [window addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

I see the log messages, so I know the code is being invoked. The indicator is at the center and 6x the default size. However, the stopAnimating isn't stopping. The only thing I can conclude is that it needs to run in the present view controller.

- (void)didStopActivity
{
    NSLog(@"%s: stopping the activityIndicator", __FUNCTION__);
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半城柳色半声笛 2024-08-26 13:12:43

您是否尝试从后台线程执行此操作?一个简单而明确的测试是添加:

NSLog(@"thread: %@", [NSThread currentThread]);

Are you trying to do this from a background thread? An easy and definitive test would be to add:

NSLog(@"thread: %@", [NSThread currentThread]);
夏尔 2024-08-26 13:12:43

检查您的 activityIndi​​cator 是否不 nil

NSLog(@"activityIndicator: %@", activityIndicator);

check if your activityIndicator isn't nil:

NSLog(@"activityIndicator: %@", activityIndicator);
风月客 2024-08-26 13:12:43

好的。我做了我的实验,果然,当我将活动指示器添加为视图控制器视图的子视图时,它完美地工作了。然而,当我使用这个新的 simpleton 项目尝试将其作为窗口的子视图时,它也起作用了。显然我的代码中有一个错误,需要更多检查。

我将向两位 Eimantas 授予答案,因为调试概念对我的解决方案很有帮助。

OK. I did my experiment and sure enough, it worked flawlessly when I added the activity indicator as a subview of viewcontroller's view. However, when I then used that new simpleton project to try it as a subview to window it also worked. Obviously a bug in my code and it needs more inspection.

I will award both Eimantas with the answer as the debug notion was helpful in my solution.

悲喜皆因你 2024-08-26 13:12:43

如果您的日志为零,则执行以下操作:

在“.h”中声明:

__strong UIActivityIndicatorView *activityIndicator

__strong 避免 ARC,然后您可以删除并停止 ActivityIndi​​cator。

[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];

if your log is nil then do this:

in ".h" declare :

__strong UIActivityIndicatorView *activityIndicator

the __strong avoid ARC and then you can remove and stop the activityIndicator.

[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
贩梦商人 2024-08-26 13:12:43

如果您将其多次添加到超级视图中,它将不起作用。

尝试

[superView addSubview:activityIndicator];

进入

if( activityIndicator == nil ) {

条件

If you are adding it to the superview more than one time it will not work.

Try putting

[superView addSubview:activityIndicator];

into the

if( activityIndicator == nil ) {

condition

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文