2015-10-08 60 views

回答

0

我很好奇這種功能的必要性,我可能會同意Chandra Vaghasiyacomment

說了這可能。

  1. 使您的視圖控制器成爲UIWebView的代表。
  2. webViewDidStartLoad:方法中,拍攝Web視圖的快照,並在視圖層次結構的Web視圖上方添加生成的視圖。
  3. webViewDidFinishLoad:webView:didFailLoadWithError:方法只是隱藏快照視圖。

示例代碼:

@interface ViewController() <UIWebViewDelegate> 
@property (weak, nonatomic) IBOutlet UIWebView *webView; 
@property (nonatomic, strong) UIView *snapshotView; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSURL *url = [NSURL URLWithString:@"https://google.com"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [self.webView loadRequest:request]; 
} 

- (void)webViewDidStartLoad:(UIWebView *)webView { 
    self.snapshotView = [self.webView snapshotViewAfterScreenUpdates:NO]; 

    self.snapshotView.frame = self.webView.frame; 

    [self.view insertSubview:self.snapshotView 
       aboveSubview:self.webView]; 

    self.snapshotView.hidden = NO; 

    // THIS IS JUST FOR DEMONSTRATION PURPOSES SO 
    // YOU CAN SEE THE LOADING HAPPENING BEHIND 
    // THE SNAPSHOT VIEW 
    self.snapshotView.alpha = 0.8; 

    NSLog(@"Display snapshot"); 
} 

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    self.snapshotView.hidden = YES; 
    NSLog(@"Hide snapshot"); 
} 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 
    self.snapshotView.hidden = YES; 
    NSLog(@"Hide snapshot due to error"); 
} 

@end 
+0

這是一個好主意THX :) –

相關問題