为什么我的 UIActivityIndicatorView 不会停止动画?
我正在尝试从我的应用程序委托管理活动指示器,这样我的任何视图都可以放置指示器。因此,我将其作为子视图添加到“窗口”并启动/停止,如下所示:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试从后台线程执行此操作?一个简单而明确的测试是添加:
Are you trying to do this from a background thread? An easy and definitive test would be to add:
检查您的
activityIndicator
是否不nil
:check if your
activityIndicator
isn'tnil
:好的。我做了我的实验,果然,当我将活动指示器添加为视图控制器视图的子视图时,它完美地工作了。然而,当我使用这个新的 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.
如果您的日志为零,则执行以下操作:
在“.h”中声明:
__strong 避免 ARC,然后您可以删除并停止 ActivityIndicator。
if your log is nil then do this:
in ".h" declare :
the __strong avoid ARC and then you can remove and stop the activityIndicator.
如果您将其多次添加到超级视图中,它将不起作用。
尝试
进入
条件
If you are adding it to the superview more than one time it will not work.
Try putting
into the
condition