YRWebView.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // YRWebView.m
  3. // UU_Ent
  4. //
  5. // Created by liujl on 2019/8/22.
  6. // Copyright © 2019 UAS. All rights reserved.
  7. //
  8. #import "YRWebView.h"
  9. @interface YRWebView()<WKNavigationDelegate>
  10. @property (nonatomic, strong) UIGestureRecognizer* popGesture;
  11. @property (nonatomic, weak) id <WKNavigationDelegate> originDelegate;
  12. @property (nonatomic, strong)UIImageView *historyView;
  13. @property (nonatomic, strong) NSMutableArray *historyStack;
  14. @property (nonatomic, assign) CGFloat panStartX;
  15. /**
  16. 用来判断是返回还是
  17. */
  18. @property (nonatomic, strong) NSMutableArray<NSString *> *urlStack;
  19. @end
  20. @implementation YRWebView
  21. - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration{
  22. self = [super initWithFrame:frame configuration:configuration];
  23. if (self) {
  24. [self defualtSet];
  25. }
  26. return self;
  27. }
  28. -(void)defualtSet{
  29. self.popGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
  30. [self addGestureRecognizer:self.popGesture];
  31. [super setNavigationDelegate:self];
  32. [YRWebView addShadowToView:self];
  33. [self addObserver:self forKeyPath:@"URL" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
  34. }
  35. + (UIImage *)screenshotOfView:(UIView *)view {
  36. UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0);
  37. if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
  38. [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
  39. }
  40. else{
  41. [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  42. }
  43. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  44. UIGraphicsEndImageContext();
  45. return image;
  46. }
  47. + (void)addShadowToView:(UIView *)view{
  48. CALayer *layer = view.layer;
  49. UIBezierPath *path = [UIBezierPath bezierPathWithRect:layer.bounds];
  50. layer.shadowPath = path.CGPath;
  51. layer.shadowColor = [UIColor blackColor].CGColor;
  52. layer.shadowOffset = CGSizeZero;
  53. layer.shadowOpacity = 0.4f;
  54. layer.shadowRadius = 8.0f;
  55. }
  56. - (void)setDelegate:(id<WKNavigationDelegate>)delegate{
  57. self.originDelegate = delegate;
  58. }
  59. - (id<WKNavigationDelegate>)delegate{
  60. return self.originDelegate;
  61. }
  62. - (void)goBack{
  63. [super goBack];
  64. [self.historyStack removeLastObject];
  65. [self.urlStack removeLastObject];
  66. }
  67. - (void)setEnablePanGesture:(BOOL)enablePanGesture{
  68. self.popGesture.enabled = enablePanGesture;
  69. }
  70. - (BOOL)enablePanGesture{
  71. return self.popGesture.enabled;
  72. }
  73. - (NSMutableArray *)historyStack {
  74. if (!_historyStack) {
  75. _historyStack = [NSMutableArray array];
  76. }
  77. return _historyStack;
  78. }
  79. - (UIImageView *)historyView{
  80. if (!_historyView) {
  81. if (self.superview) {
  82. _historyView = [[UIImageView alloc] initWithFrame:self.bounds];
  83. [self.superview insertSubview:_historyView belowSubview:self];
  84. }
  85. }
  86. return _historyView;
  87. }
  88. - (void)dealloc {
  89. [self removeObserver:self forKeyPath:@"URL"];
  90. if (self.historyView) {
  91. [self.historyView removeFromSuperview];
  92. self.historyView = nil;
  93. }
  94. }
  95. - (void)layoutSubviews {
  96. [super layoutSubviews];
  97. self.historyView.frame = self.bounds;
  98. }
  99. #pragma mark === gesture===
  100. - (void)panGesture:(UIPanGestureRecognizer *)sender{
  101. if (![self canGoBack] || self.historyStack.count == 0) {
  102. if (self.panDelegate && [self.panDelegate respondsToSelector:@selector(panableWebView:panPopGesture:)]) {
  103. [self.panDelegate panableWebView:self panPopGesture:sender];
  104. }
  105. return;
  106. }
  107. CGPoint point = [sender translationInView:self];
  108. if (sender.state == UIGestureRecognizerStateBegan) {
  109. _panStartX = point.x;
  110. }
  111. else if (sender.state == UIGestureRecognizerStateChanged){
  112. CGFloat deltaX = point.x - _panStartX;
  113. if (deltaX > 0) {
  114. if ([self canGoBack]) {
  115. assert(self.historyStack.count > 0);
  116. NSDictionary *history = self.historyStack.lastObject;
  117. self.historyView.image = [history objectForKey:@"preview"];
  118. self.x = deltaX;
  119. self.historyView.x = -self.width / 2.0f + deltaX / 2.0f;
  120. }
  121. }
  122. }
  123. else if (sender.state == UIGestureRecognizerStateEnded){
  124. CGFloat deltaX = point.x - _panStartX;
  125. CGFloat duration = .5f;
  126. if ([self canGoBack]) {
  127. if (deltaX > self.width / 4.0f) {
  128. [UIView animateWithDuration:(1.0f - deltaX / self.width) * duration animations:^{
  129. self.x = self.width;
  130. self.historyView.x = 0;
  131. [self goBack];
  132. } completion:^(BOOL finished) {
  133. self.x = 0;
  134. [self.historyView removeFromSuperview];
  135. self.historyView = nil;
  136. }];
  137. }
  138. else{
  139. [UIView animateWithDuration:(deltaX/self.bounds.size.width)*duration animations:^{
  140. CGRect rc = self.frame;
  141. rc.origin.x = 0;
  142. self.frame = rc;
  143. rc.origin.x = -self.bounds.size.width/2.0f;
  144. self.historyView.frame = rc;
  145. } completion:^(BOOL finished) {
  146. }];
  147. }
  148. }
  149. }
  150. }
  151. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  152. if ([keyPath isEqualToString:@"URL"]) {
  153. NSString *newUrl = [NSString stringWithFormat:@"%@",change[@"new"]];
  154. //执行的是跳入新页面的操作
  155. if (![self.urlStack containsObject:newUrl] && [newUrl containsString:@"http"]) {
  156. [self.urlStack addObject:newUrl];
  157. }
  158. }
  159. }
  160. -(void)setUrl:(NSString *)url{
  161. _url = url;
  162. [self.urlStack addObject:url];
  163. }
  164. -(NSMutableArray<NSString *> *)urlStack{
  165. if (!_urlStack) {
  166. _urlStack = [NSMutableArray new];
  167. }
  168. return _urlStack;
  169. }
  170. @end