@@ -13,14 +13,13 @@ @implementation SideRefresh
1313
1414- (void )willMoveToSuperview : (UIView *)newSuperview {
1515 [super willMoveToSuperview: newSuperview];
16+ [self removeObserver ];
1617 if ([newSuperview isKindOfClass: [UICollectionView class ]]) {
1718 self.collectionView = (UICollectionView *)newSuperview;
1819 self.collectionView .alwaysBounceHorizontal = YES ;
1920 self.originalContentInset = self.collectionView .side_inset ;
2021 self.collectionViewPageEnabel = self.collectionView .pagingEnabled ;
21- NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
22- [self .collectionView addObserver: self forKeyPath: @" contentOffset" options: options context: nil ];
23- [self .collectionView addObserver: self forKeyPath: @" contentSize" options: options context: nil ];
22+ [self addObserver ];
2423 }
2524}
2625
@@ -38,9 +37,15 @@ - (void)endLoading {
3837 self.refreshStatus = SideRefreshStatusNormal;
3938}
4039
41- - (void )dealloc {
42- [self .collectionView removeObserver: self forKeyPath: @" contentOffset" ];
43- [self .collectionView removeObserver: self forKeyPath: @" contentSize" ];
40+ - (void )removeObserver {
41+ [self .superview removeObserver: self forKeyPath: @" contentOffset" ];
42+ [self .superview removeObserver: self forKeyPath: @" contentSize" ];
43+ }
44+
45+ - (void )addObserver {
46+ NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
47+ [self .collectionView addObserver: self forKeyPath: @" contentOffset" options: options context: nil ];
48+ [self .collectionView addObserver: self forKeyPath: @" contentSize" options: options context: nil ];
4449}
4550
4651#pragma mark - ContentOffset Observer
0 commit comments