WardTipView.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // WardTipView.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/2/11.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "WardTipView.h"
  9. @interface WardTipView()<WKUIDelegate,WKNavigationDelegate>
  10. @property (nonatomic, strong) WKWebView *webView;
  11. @property (nonatomic, strong) UIView *shadowView;
  12. @property (nonatomic, copy) tipWebViewDidFinishLoadBlock tipWebViewDidFinishLoadBlock;
  13. @end
  14. @implementation WardTipView
  15. - (instancetype)initWithFrame:(CGRect)frame{
  16. if (self = [super initWithFrame:frame]) {
  17. self.alpha = 0;
  18. self.layer.shadowColor = kBlackColor.CGColor;
  19. self.layer.shadowOffset = CGSizeMake(2, 5);
  20. self.layer.shadowOpacity = 0.5;
  21. [self initSubview];
  22. }
  23. return self;
  24. }
  25. - (void)initSubview{
  26. [self addSubview:self.webView];
  27. }
  28. #pragma mark - WkWebViewDelegate
  29. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  30. [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  31. CGFloat documentHeight = [result doubleValue];
  32. CGRect webFrame = webView.frame;
  33. webFrame.size.height = documentHeight;
  34. webView.frame = webFrame;
  35. self.height = documentHeight;
  36. }];
  37. }
  38. - (void)show:(UIView *)superView{
  39. [superView addSubview:self.shadowView];
  40. [superView addSubview:self];
  41. [UIView animateWithDuration:0.25 animations:^{
  42. self.frame = CGRectMake(80, kScreenH / 4, kScreenW - 160, kScreenH / 2);
  43. self.alpha = 1;
  44. }];
  45. }
  46. - (void)hide{
  47. [UIView animateWithDuration:0.25 animations:^{
  48. self.alpha = 0;
  49. self.frame = CGRectMake(80, kScreenH, kScreenW - 160, kScreenH / 2);
  50. } completion:^(BOOL finished) {
  51. [self.shadowView removeFromSuperview];
  52. [self removeFromSuperview];
  53. }];
  54. }
  55. - (void)setURL:(NSString *)url{
  56. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&id=%@",self.BuguLive.appModel.h5_url.guartian_special_effects,url]]]];
  57. NSLog(@"%@&id=%@",self.BuguLive.appModel.h5_url.guartian_special_effects,url);
  58. }
  59. - (void)setTipWebViewDidFinishLoadBlock:(tipWebViewDidFinishLoadBlock)tipWebViewDidFinishLoadBlock{
  60. _tipWebViewDidFinishLoadBlock = tipWebViewDidFinishLoadBlock;
  61. }
  62. - (WKWebView *)webView{
  63. if (!_webView) {
  64. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  65. _webView = [[WKWebView alloc]initWithFrame:self.bounds configuration:config];
  66. _webView.UIDelegate = self;
  67. _webView.navigationDelegate = self;
  68. _webView.scrollView.showsVerticalScrollIndicator = NO;
  69. _webView.scrollView.showsHorizontalScrollIndicator = NO;
  70. }
  71. return _webView;
  72. }
  73. - (UIView *)shadowView{
  74. if (!_shadowView) {
  75. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  76. _shadowView.backgroundColor = kClearColor;
  77. _shadowView.userInteractionEnabled = YES;
  78. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  79. [_shadowView addGestureRecognizer:tap];
  80. }
  81. return _shadowView;
  82. }
  83. /*
  84. // Only override drawRect: if you perform custom drawing.
  85. // An empty implementation adversely affects performance during animation.
  86. - (void)drawRect:(CGRect)rect {
  87. // Drawing code
  88. }
  89. */
  90. @end