| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- //
- // YRWebView.m
- // UU_Ent
- //
- // Created by liujl on 2019/8/22.
- // Copyright © 2019 UAS. All rights reserved.
- //
- #import "YRWebView.h"
- @interface YRWebView()<WKNavigationDelegate>
- @property (nonatomic, strong) UIGestureRecognizer* popGesture;
- @property (nonatomic, weak) id <WKNavigationDelegate> originDelegate;
- @property (nonatomic, strong)UIImageView *historyView;
- @property (nonatomic, strong) NSMutableArray *historyStack;
- @property (nonatomic, assign) CGFloat panStartX;
- /**
- 用来判断是返回还是
- */
- @property (nonatomic, strong) NSMutableArray<NSString *> *urlStack;
- @end
- @implementation YRWebView
- - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration{
-
- self = [super initWithFrame:frame configuration:configuration];
-
- if (self) {
-
- [self defualtSet];
-
- }
-
- return self;
- }
- -(void)defualtSet{
-
- self.popGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
-
- [self addGestureRecognizer:self.popGesture];
-
- [super setNavigationDelegate:self];
-
- [YRWebView addShadowToView:self];
-
- [self addObserver:self forKeyPath:@"URL" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
-
- }
- + (UIImage *)screenshotOfView:(UIView *)view {
-
- UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0);
-
-
-
- if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
-
- [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
-
- }
-
- else{
-
- [view.layer renderInContext:UIGraphicsGetCurrentContext()];
-
- }
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
-
- UIGraphicsEndImageContext();
-
- return image;
-
- }
- + (void)addShadowToView:(UIView *)view{
-
- CALayer *layer = view.layer;
-
- UIBezierPath *path = [UIBezierPath bezierPathWithRect:layer.bounds];
-
- layer.shadowPath = path.CGPath;
-
- layer.shadowColor = [UIColor blackColor].CGColor;
-
- layer.shadowOffset = CGSizeZero;
-
- layer.shadowOpacity = 0.4f;
-
- layer.shadowRadius = 8.0f;
-
- }
- - (void)setDelegate:(id<WKNavigationDelegate>)delegate{
-
- self.originDelegate = delegate;
-
- }
- - (id<WKNavigationDelegate>)delegate{
-
- return self.originDelegate;
-
- }
- - (void)goBack{
-
- [super goBack];
-
- [self.historyStack removeLastObject];
- [self.urlStack removeLastObject];
-
- }
- - (void)setEnablePanGesture:(BOOL)enablePanGesture{
-
- self.popGesture.enabled = enablePanGesture;
-
- }
- - (BOOL)enablePanGesture{
-
- return self.popGesture.enabled;
-
- }
- - (NSMutableArray *)historyStack {
-
- if (!_historyStack) {
-
- _historyStack = [NSMutableArray array];
-
- }
-
- return _historyStack;
-
- }
- - (UIImageView *)historyView{
-
- if (!_historyView) {
-
- if (self.superview) {
-
- _historyView = [[UIImageView alloc] initWithFrame:self.bounds];
-
- [self.superview insertSubview:_historyView belowSubview:self];
-
- }
-
- }
-
-
-
- return _historyView;
-
- }
- - (void)dealloc {
-
-
- [self removeObserver:self forKeyPath:@"URL"];
-
- if (self.historyView) {
-
- [self.historyView removeFromSuperview];
-
- self.historyView = nil;
-
- }
-
- }
- - (void)layoutSubviews {
-
- [super layoutSubviews];
-
- self.historyView.frame = self.bounds;
-
- }
- #pragma mark === gesture===
- - (void)panGesture:(UIPanGestureRecognizer *)sender{
-
- if (![self canGoBack] || self.historyStack.count == 0) {
-
- if (self.panDelegate && [self.panDelegate respondsToSelector:@selector(panableWebView:panPopGesture:)]) {
-
- [self.panDelegate panableWebView:self panPopGesture:sender];
-
- }
-
-
-
- return;
-
- }
-
-
-
- CGPoint point = [sender translationInView:self];
-
- if (sender.state == UIGestureRecognizerStateBegan) {
-
- _panStartX = point.x;
-
- }
-
- else if (sender.state == UIGestureRecognizerStateChanged){
-
- CGFloat deltaX = point.x - _panStartX;
-
- if (deltaX > 0) {
-
- if ([self canGoBack]) {
-
- assert(self.historyStack.count > 0);
-
- NSDictionary *history = self.historyStack.lastObject;
-
- self.historyView.image = [history objectForKey:@"preview"];
-
- self.x = deltaX;
-
- self.historyView.x = -self.width / 2.0f + deltaX / 2.0f;
-
- }
-
- }
-
- }
-
- else if (sender.state == UIGestureRecognizerStateEnded){
-
- CGFloat deltaX = point.x - _panStartX;
-
- CGFloat duration = .5f;
-
- if ([self canGoBack]) {
-
- if (deltaX > self.width / 4.0f) {
-
- [UIView animateWithDuration:(1.0f - deltaX / self.width) * duration animations:^{
-
-
- self.x = self.width;
-
- self.historyView.x = 0;
-
- [self goBack];
-
-
- } completion:^(BOOL finished) {
-
- self.x = 0;
-
- [self.historyView removeFromSuperview];
-
- self.historyView = nil;
-
-
-
- }];
-
- }
-
- else{
-
- [UIView animateWithDuration:(deltaX/self.bounds.size.width)*duration animations:^{
-
- CGRect rc = self.frame;
-
- rc.origin.x = 0;
-
- self.frame = rc;
-
- rc.origin.x = -self.bounds.size.width/2.0f;
-
- self.historyView.frame = rc;
-
- } completion:^(BOOL finished) {
-
-
-
- }];
-
- }
-
- }
-
- }
-
- }
- -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
-
- if ([keyPath isEqualToString:@"URL"]) {
-
- NSString *newUrl = [NSString stringWithFormat:@"%@",change[@"new"]];
- //执行的是跳入新页面的操作
- if (![self.urlStack containsObject:newUrl] && [newUrl containsString:@"http"]) {
-
- [self.urlStack addObject:newUrl];
-
- }
-
- }
-
- }
- -(void)setUrl:(NSString *)url{
-
- _url = url;
-
- [self.urlStack addObject:url];
-
-
- }
- -(NSMutableArray<NSString *> *)urlStack{
-
- if (!_urlStack) {
-
- _urlStack = [NSMutableArray new];
-
- }
-
- return _urlStack;
- }
- @end
|