@@ -17,6 +17,7 @@ @interface STSpringScrollView ()
1717@property (nonatomic , assign ) BOOL orgScrollEnabled;
1818@property (nonatomic , copy ) NSDictionary *initialContentOffset;
1919@property (nonatomic , assign ) BOOL allLoaded;
20+ @property (nonatomic , assign ) BOOL initialed;
2021@end
2122
2223@implementation STSpringScrollView
@@ -29,9 +30,22 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher{
2930}
3031
3132- (void )setInitialContentOffset : (NSDictionary *)initialContentOffset {
32- if (!_initialContentOffset && initialContentOffset) {
33- _initialContentOffset = initialContentOffset;
34- [self .scrollView setContentOffset: CGPointMake ([[initialContentOffset objectForKey: @" x" ] floatValue ], [[initialContentOffset objectForKey: @" y" ] floatValue ])];
33+ _initialContentOffset = initialContentOffset;
34+ if (!self.initialed ) {
35+ self.initialed = YES ;
36+ [self .scrollView addObserver: self forKeyPath: @" contentSize" options: NSKeyValueObservingOptionNew context: nil ];
37+ }
38+ }
39+
40+ - (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary <NSKeyValueChangeKey,id> *)change context : (void *)context {
41+ CGSize size = [change[NSKeyValueChangeNewKey ] CGSizeValue ];
42+ float x = [[self .initialContentOffset objectForKey: @" x" ] floatValue ];
43+ float y = [[self .initialContentOffset objectForKey: @" y" ] floatValue ];
44+ if ([keyPath isEqualToString: @" contentSize" ] && size.width > x && size.height >y ) {
45+ [self .scrollView removeObserver: self forKeyPath: @" contentSize" ];
46+ dispatch_async (dispatch_get_main_queue (), ^{
47+ [self .scrollView setContentOffset: CGPointMake (x, y)];
48+ });
3549 }
3650}
3751
@@ -73,7 +87,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
7387- (void )scrollViewDidEndScrollingAnimation : (UIScrollView *)scrollView {
7488 if (!UIEdgeInsetsEqualToEdgeInsets (scrollView.contentInset , self.orgInsets ) && ([self hitRefreshStatus: @[@" waiting" ]] || [self hitLoadingStatus: @[@" waiting" ]])) {
7589 scrollView.contentInset = self.orgInsets ;
76- // scrollView.scrollEnabled = self.orgScrollEnabled;
90+ // scrollView.scrollEnabled = self.orgScrollEnabled;
7791 }
7892}
7993
@@ -82,16 +96,16 @@ - (void)endRefresh {
8296 self.refreshStatus = @" rebound" ;
8397 [self .scrollView setContentOffset: CGPointMake (0 , 0 ) animated: YES ];
8498 self.orgScrollEnabled = self.scrollView .scrollEnabled ;
85- // self.scrollView.scrollEnabled = NO;
99+ // self.scrollView.scrollEnabled = NO;
86100 }
87101}
88102
89103- (void )endLoading {
90104 if ([self hitLoadingStatus: @[@" loading" ]]) {
91105 self.loadingStatus = @" rebound" ;
92106 [self .scrollView setContentOffset: CGPointMake (0 , self .scrollView.contentSize.height-self .bounds.size.height) animated: YES ];
93- // self.orgScrollEnabled = self.scrollView.scrollEnabled;
94- // self.scrollView.scrollEnabled = NO;
107+ // self.orgScrollEnabled = self.scrollView.scrollEnabled;
108+ // self.scrollView.scrollEnabled = NO;
95109 }
96110}
97111
0 commit comments