admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
//  WebControllerView.m
//  BuWanVideo2.0
//
//  Created by weikou2016 on 16/8/17.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "WebControllerView.h"
#import <WebKit/WebKit.h>
 
@interface WebControllerView () <WKUIDelegate, WKNavigationDelegate> {
    WKWebView * webview;
    UIButton *button2;
    CABasicAnimation *animation;
}
 
@end
 
@implementation WebControllerView
@synthesize url;
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initScene];
}
 
-(void)initScene {
    
    self.navigationItem.title = @"发现";
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];   //返回上个网页
    [button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 32, 32);
    UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = iconBarItem;
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];    //退出页面
    [button1 setTitle:@"关闭" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(webback) forControlEvents:UIControlEventTouchUpInside];
    button1.frame = CGRectMake(100, 32, 30, 18);
    button1.titleLabel.font = [UIFont systemFontOfSize:14];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *bar1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
    UIBarButtonItem *bar2 = self.navigationItem.leftBarButtonItem;
    NSArray *arr = @[bar2,bar1];
    self.navigationItem.leftBarButtonItems = arr;
    
    button2 = [UIButton buttonWithType:UIButtonTypeCustom];    //退出页面
    [button2 setImage:[[UIImage imageNamed:@"网页刷新"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(refurbish) forControlEvents:UIControlEventTouchUpInside];
    button2.frame = CGRectMake(100, 32, 32, 32);
    UIBarButtonItem *bar3 = [[UIBarButtonItem alloc]initWithCustomView:button2];
    self.navigationItem.rightBarButtonItem = bar3;
    
    webview = [[WKWebView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
    webview.UIDelegate = self;
    webview.navigationDelegate = self;
    
    [self.view addSubview:webview];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webview loadRequest:request];
    
    [self beginrotate];
}
 
-(void)webback{
    [self.navigationController popViewControllerAnimated:YES];
}
 
-(void)back{
    if([webview canGoBack]){
        [webview goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
 
-(void)refurbish {
    [webview reload];
}
 
//9.0才能使用,web内容处理中断时会触发
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
    [webView reload];
}
 
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    if(!animation){
        [self beginrotate];
    }
}
 
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
}
 
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [self stoprotate];
}
 
-(void)beginrotate{
    animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.toValue = [NSNumber numberWithFloat:-M_PI*2.0];
    animation.duration = 1;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    [button2.layer addAnimation:animation forKey:@"rotationAnimation"];
}
 
- (void)stoprotate {
    [button2.layer removeAllAnimations];
    animation = nil;
}
@end