活动指示器视图没有旋转

9

我有下面的代码,其中我正在加载一些链接到webview中。

- (void)viewDidLoad
{
    mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self openWebPage:fileName];
    [self.view addSubview:myWebView];
    [self.view addSubview:mySpinner];
    mySpinner.center = CGPointMake(self.view.frame.size.width / 2.0, 100);
}

-(void)openWebPage:(NSString*)address {
    NSURL*url=[NSURL URLWithString:address];
    NSURLRequest*request=[NSURLRequest requestWithURL:url];
    myWebView.scalesPageToFit = NO;

    [myWebView loadRequest:request];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error!!!" message:@"Please make sure you are connected to 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [errorView show];
    mySpinner.hidden = YES;
    [mySpinner stopAnimating];
}


-(void)webViewDidStartLoad:(UIWebView *)webView2 {
    NSLog(@"webViewDidStartLoad");
    mySpinner.hidden = NO;
    [mySpinner startAnimating];
    NSLog(@"step 1");
    NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 2 ];
    [NSThread sleepUntilDate:future];
    NSLog(@"step 2");
}

-(void)webViewDidFinishLoad:(UIWebView *)webView2 {
    NSLog(@"webViewDidFinishLoad");
    mySpinner.hidden = YES;
    [mySpinner stopAnimating];

}

我正在做的事情是在webViewDidStartLoad时,使用[mySpinner startAnimating];显示旋转器并开始动画,但它没有旋转。 它只停留在原地(没有旋转)。
有什么想法出了什么问题吗?

编辑1

我有webview代理@interface WebDetailsViewController : UIViewController<UIWebViewDelegate> 此外,我添加了[NSThread sleepUntilDate:future];,只是为了验证活动指示器视图是否正在动画。
下面是我从NSLog中获取的内容。
2013-06-23 16:29:28.843 GulfLab[2048:907] webViewDidStartLoad
2013-06-23 16:29:28.845 GulfLab[2048:907] step 1
2013-06-23 16:29:30.847 GulfLab[2048:907] step 2
2013-06-23 16:29:31.836 GulfLab[2048:907] webViewDidFinishLoad

编辑2

问题出在下面这行代码...

[UIView  beginAnimations: @"Showinfo"context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController: secondView animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

以下是我所拥有的...
在第一个视图控制器中,我有一些按钮,当我单击这些按钮时,会跳转到第二个视图控制器,在那里我会根据所按下的按钮显示Web文件。
我的客户想要在转到第二个视图时添加一些效果,因此我添加了上面的代码。但由于这个原因,我遇到了活动问题。
你有什么想法需要改变吗?
进一步调查后,我发现问题出在这行代码...
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];

但我需要这行代码,否则动画不会发生...

今天我还注意到,如果我触摸webview,它会开始动画

烦死了...有人能帮帮我吗?


你是否忘记添加webview代理? - Swapnil
你不想在主线程上睡觉,因为那会阻塞该线程(UI代码运行的线程)。 - Rob
@Rob:我只是为了检查活动指示器是否正常工作而添加了睡眠功能... - Fahim Parkar
@Swapnil:是的,我有... - Fahim Parkar
你可以尝试添加 [myWebView addSubview:mySpinner]; 而不是添加 [self.view addSubview:mySpinner]; - Swapnil
显示剩余7条评论
6个回答

23
如果有人遇到与此类似的问题,其中指示器没有旋转:
请确保您的storyboard中设置了Animating属性。 Activity Indicator Animating

5

可能你无法使其动画是因为你没有在主 UI 线程上执行。尝试将其添加到主循环的调度队列中,以强制它在主线程上运行,这样就可以进行动画了。

dispatch_async(dispatch_get_main_queue(), ^{
    [mySpinner startAnimating];
}];

2
尝试在alloc init之后放置此代码。
mySpinner.hidesWhenStopped = false;

0

在你的代码中删除 mySpinner.hidden = YES/NO;

只需在 viewDidload 中添加 [mySpinner setHidesWhenStopped:YES];

然后你就可以开始和停止了。

- (void)viewDidLoad
{
    mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [mySpinner setHidesWhenStopped:YES];//set Hidden here

    [self openWebPage:fileName];
    [self.view addSubview:myWebView];
    [self.view addSubview:mySpinner];
    [self.view bringSubviewToFront:mySpinner]; //Add if still not show
}

也许你的布局超出了范围,可以尝试使用以下代码进行测试:mySpinner.center = self.view.center; - Alex

0
这是我在代码中使用旋转器实现弹出窗口的方法。虽然不完全符合您的要求,但有些相似。
在您的*.h文件中:
@interface v3ViewController : UIViewController 
{
    UIAlertView *megaAlert;
}

@property (nonatomic, retain) UIAlertView *megaAlert;

- (IBAction) invokeMegaAnnoyingPopup;
- (IBAction) dismissMegaAnnoyingPopup;

在你的 *.m 文件中:

@synthesize megaAlert;

- (IBAction) dismissMegaAnnoyingPopup
{
    [self.megaAlert dismissWithClickedButtonIndex:0 animated:YES];
    self.megaAlert = nil;
}

- (IBAction) invokeMegaAnnoyingPopup
{
    self.megaAlert = [[UIAlertView alloc] initWithTitle:@"Please wait..."
                                                message:nil delegate:self cancelButtonTitle:nil
                                      otherButtonTitles: nil];

    [self.megaAlert show];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]
                                          initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator.center = CGPointMake(130, 70);


    [indicator startAnimating];
    [self.megaAlert addSubview:indicator];
}

0
我真的认为在这种情况下,[NSThread sleepUntilDate:future]; 这个调用正在让你疲于应对。我的大部分代码都是与第三方硬件集成的,这个硬件在某些情况下会阻塞主线程,当发生这种情况时,你可能会遇到类似的行为,即界面上的一个元素出现,但稍微卡住,直到你要么点击屏幕来"启动"界面,要么阻塞的调用完成。
如果我是你,我会首先去掉sleepUntilDate这个调用,然后移除停止mySpinner的调用,以确保它实际上正在旋转,这样当你遇到较长的webViewDidFinishLoad调用时,你就可以确信它正在工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接