WYWebView.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // WYWebView.m
  3. // RedSunProperty
  4. //
  5. // Created by WuShiHai'sMac on 8/5/15.
  6. // Copyright (c) 2015 XiTai. All rights reserved.
  7. //
  8. #import "WYWebView.h"
  9. NSString *const WYRefreshKeyPathContentOffsetContext = @"WYRefreshKeyPathContentOffsetContext";
  10. @interface WYWebView ()
  11. @property (nonatomic, assign) CGSize lastContentSize;
  12. @property (nonatomic, assign) long long lastResopnseDelegateTime;
  13. @end
  14. @implementation WYWebView
  15. - (instancetype)initWithFrame:(CGRect)frame{
  16. if (self = [super initWithFrame:frame]) {
  17. [self addObservers];
  18. }
  19. return self;
  20. }
  21. - (void)addObservers{
  22. [self.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:(__bridge void *)(WYRefreshKeyPathContentOffsetContext)];
  23. }
  24. - (void)removeObservers{
  25. [self.scrollView removeObserver:self forKeyPath:@"contentSize" context:(__bridge void *)(WYRefreshKeyPathContentOffsetContext)];
  26. }
  27. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  28. {
  29. // NSLog(@"fasnfasnfansf,ansf:%.f",self.scrollView.contentSize.height);
  30. // if (CGSizeEqualToSize(self.lastContentSize, self.scrollView.contentSize)) {
  31. // return;
  32. // }
  33. //
  34. //// NSLog(@"asdfasfd%.f",self.scrollView.contentSize.height);
  35. //
  36. // long long time = [NSDate date].timeIntervalSince1970 * 1000;
  37. // if (time - self.lastResopnseDelegateTime <= 500) {
  38. // [self performSelector:@selector(appendChangeSize) withObject:nil afterDelay:0.5];
  39. // return;
  40. // }
  41. // self.lastResopnseDelegateTime = time;
  42. //
  43. // self.lastContentSize = self.scrollView.contentSize;
  44. if ([_wy_delegate respondsToSelector:@selector(webViewDidContentSizeChange:)]) {
  45. [_wy_delegate webViewDidContentSizeChange:self];
  46. }
  47. }
  48. - (void)appendChangeSize{
  49. long long time = [NSDate date].timeIntervalSince1970 * 1000;
  50. //说明有遗漏通知的
  51. if (self.lastResopnseDelegateTime < time) {
  52. self.lastResopnseDelegateTime = time;
  53. if ([_wy_delegate respondsToSelector:@selector(webViewDidContentSizeChange:)]) {
  54. [_wy_delegate webViewDidContentSizeChange:self];
  55. }
  56. }
  57. }
  58. /*
  59. // Only override drawRect: if you perform custom drawing.
  60. // An empty implementation adversely affects performance during animation.
  61. - (void)drawRect:(CGRect)rect {
  62. // Drawing code
  63. }
  64. */
  65. - (void)dealloc{
  66. [self removeObservers];
  67. }
  68. @end