活动指示器不旋转

发布于 2024-10-08 18:31:03 字数 858 浏览 0 评论 0原文

我正在尝试向我的应用程序添加一个旋转活动指示器(UIActivityIndi​​catorView),同时解析来自互联网的数据。我有一个 IBOutlet(旋转器)连接到 IB 中的 UIActivityIndi​​catorView。最初我是这样设置的:

-

 (void) function {
        self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
 self.spinner.hidesWhenStopped = YES;
 [spinner startAnimating];
 //parse data from internet
 [spinner stopAnimating];}

但是旋转器不旋转。我读到这与所有内容都在同一线程上有关。所以我尝试了这个:

    - (void) newFunction {
        self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
 self.spinner.hidesWhenStopped = YES;
 [spinner startAnimating];
 [NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
 [spinner stopAnimating];}

但仍然没有运气。有什么想法吗?谢谢。

I'm trying to add a spinning activity indicator (UIActivityIndicatorView) to my app while it parses data from the internet. I have an IBOutlet (spinner) connected to a UIActivityIndicatorView in IB. Initially I had it set up like this:

-

 (void) function {
        self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
 self.spinner.hidesWhenStopped = YES;
 [spinner startAnimating];
 //parse data from internet
 [spinner stopAnimating];}

But the spinner wouldn't spin. I read that it had something to do with everything being on the same thread. So I tried this:

    - (void) newFunction {
        self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
 self.spinner.hidesWhenStopped = YES;
 [spinner startAnimating];
 [NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
 [spinner stopAnimating];}

But still no luck. Any ideas? Thanks.

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

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

发布评论

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

评论(3

苏佲洛 2024-10-15 18:31:03

您的 newFunction: 方法应如下所示:

- (void) newFunction {
   self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
   self.spinner.hidesWhenStopped = YES;
   [NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
}

并且您的 function 方法应如下所示:

- (void) function {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   [self.spinner performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];

   //...

   [self.spinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
   [pool drain];
}

Your newFunction: method should look like this:

- (void) newFunction {
   self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
   self.spinner.hidesWhenStopped = YES;
   [NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
}

And your function method should look like this:

- (void) function {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   [self.spinner performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];

   //...

   [self.spinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
   [pool drain];
}
别低头,皇冠会掉 2024-10-15 18:31:03

您不应该再次初始化指示器。请用此替换您的代码。

-(void) function {
    [spinner startAnimating];
    [self performSelector:@selector(newfunction) withObject:nil afterDelay:3.0];
}
- (void) newfunction {
     [spinner stopAnimating];
}

谢谢。

you should not intitialize indicator again .please replace your code with this.

-(void) function {
    [spinner startAnimating];
    [self performSelector:@selector(newfunction) withObject:nil afterDelay:3.0];
}
- (void) newfunction {
     [spinner stopAnimating];
}

Thanks.

凯凯我们等你回来 2024-10-15 18:31:03

只需查看“//parse data from internet”是同步还是异步即可。异步意味着一个单独的线程将从该点开始,并且当前函数的执行将立即继续。

在第二个示例中,您显式创建单独的线程,这意味着 @selector(function) 将在单独的线程上发生,下一个语句 [spinner stopAnimating] 是立即执行。所以,旋转器似乎根本没有旋转。

此外,请确保仅在主线程上启动和停止活动指示器。

Just see that the "//parse data from internet " is synchronous or asynchronous. Asynchronous would mean that a separate thread would start from that point on, and the current function execution will continue without delay.

In your second example, you are explicitly making separate thread, which means that @selector(function) will happen on a separate thread, and the next statement [spinner stopAnimating] is executed immediately. So, it seems like spinner is not spinning at all.

Moreover, make sure you start and stop the activity indicator on main thread only.

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