我试图在加载 UIWebView 时将 UIImageView 用作动画,并在加载完成后删除该视图。但是,当 webview 开始和停止加载时,我无法让它开始/停止动画。代码片段:
@synthesize webView; //UIWebView
@synthesize loadView; //UIImageView
- (void)viewDidLoad {
// ...
loadView = [[UIImageView alloc] initWithFrame:[self.view bounds]];
[loadView setAnimationImages:[NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],
nil]];
[loadView setTag:1];
[loadView setAnimationDuration:1.0f];
[loadView setAnimationRepeatCount:0];
[self.view addSubview:loadView];
[webView loadRequest:request]; //request is defined somewhere else
}
- (void)webViewDidStartLoad:(UIWebView *)curWebView {
[loadView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)curWebView {
[loadView removeFromSuperview];
}
- (void)webView:(UIWebView *)curWebView didFailLoadWithError:(NSError *)error {
[loadView stopAnimating];
// ... Code to show reload button
}
如果我打电话[loadView startAnimating]
,viewDidLoad
它会动画,但[loadView stopAnimating]
仍然webView:didFailLoadWithError
无法正常工作。否则,laodView 只显示一个空白的白屏。