[self prepare];//在父類initWithFrame方法調用 [self placeSubviews];//在父類layoutSubviews方法調用
類繼承:父類定義了方法的基本實現,子類在此基礎上進行持續增長,達到複雜功能。與模版方法的區別是沒有固定的執行步驟。設計模式
//狀態設置 - (void)setState:(MJRefreshState)state //事件監聽 - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{} - (void)scrollViewContentSizeDidChange:(NSDictionary *)change{} - (void)scrollViewPanStateDidChange:(NSDictionary *)change{}
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{}
- (void)prepare { [super prepare]; // 設置key self.lastUpdatedTimeKey = MJRefreshHeaderLastUpdatedTimeKey; // 設置高度 self.mj_h = MJRefreshHeaderHeight; } - (void)placeSubviews { [super placeSubviews]; // 設置y值(當本身的高度發生改變了,確定要從新調整Y值,因此放到placeSubviews方法中設置y值) self.mj_y = - self.mj_h - self.ignoredScrollViewContentInsetTop; }
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change { [super scrollViewContentOffsetDidChange:change]; // 在刷新的refreshing狀態 if (self.state == MJRefreshStateRefreshing) { // 暫時保留 //My:當NavigationBar從一個頁面滑出時,可能被移除頁面,其window爲nil if (self.window == nil) return; // sectionheader停留解決 //My:當scrollView向下偏移的距離超過它的contentInset的上間隔時,取距離大的 CGFloat insetT = - self.scrollView.mj_offsetY > _scrollViewOriginalInset.top ? - self.scrollView.mj_offsetY : _scrollViewOriginalInset.top; //My:當這個距離超過了(刷新控件的高度 + 它的contentInset的上間隔)時,取它們的和值 insetT = insetT > self.mj_h + _scrollViewOriginalInset.top ? self.mj_h + _scrollViewOriginalInset.top : insetT; //My:將這個合理的最大值,設置到它的contentInset的上間隔上。 self.scrollView.mj_insetT = insetT; //My:實際露出的刷新空間高 self.insetTDelta = _scrollViewOriginalInset.top - insetT; return; } // 跳轉到下一個控制器時,contentInset可能會變 _scrollViewOriginalInset = self.scrollView.mj_inset; // 當前的contentOffset CGFloat offsetY = self.scrollView.mj_offsetY; // 頭部控件恰好出現的offsetY CGFloat happenOffsetY = - self.scrollViewOriginalInset.top; // 若是是向上滾動到看不見頭部控件,直接返回 // >= -> > if (offsetY > happenOffsetY) return; // 普通 和 即將刷新 的臨界點 //My:下拉距離正好是(刷新控件高度+contentInset的上間隔) CGFloat normal2pullingOffsetY = happenOffsetY - self.mj_h; //My:露出的高度/總高度 CGFloat pullingPercent = (happenOffsetY - offsetY) / self.mj_h; if (self.scrollView.isDragging) { // 若是正在拖拽 self.pullingPercent = pullingPercent; if (self.state == MJRefreshStateIdle && offsetY < normal2pullingOffsetY) { //My:下拉度超過臨界值 // 轉爲即將刷新狀態 self.state = MJRefreshStatePulling; } else if (self.state == MJRefreshStatePulling && offsetY >= normal2pullingOffsetY) { //My:下拉度小於臨界值 // 轉爲普通狀態 self.state = MJRefreshStateIdle; } } else if (self.state == MJRefreshStatePulling) { // 即將刷新 && 手鬆開 // 開始刷新 [self beginRefreshing]; } else if (pullingPercent < 1) { self.pullingPercent = pullingPercent; } }
- (void)setState:(MJRefreshState)state { MJRefreshCheckState // 根據狀態作事情 if (state == MJRefreshStateIdle) { if (oldState != MJRefreshStateRefreshing) return; // 保存刷新時間 [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:self.lastUpdatedTimeKey]; [[NSUserDefaults standardUserDefaults] synchronize]; // 恢復inset和offset [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{ self.scrollView.mj_insetT += self.insetTDelta; // 自動調整透明度 if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0; } completion:^(BOOL finished) { self.pullingPercent = 0.0; if (self.endRefreshingCompletionBlock) { self.endRefreshingCompletionBlock(); } }]; } else if (state == MJRefreshStateRefreshing) { MJRefreshDispatchAsyncOnMainQueue({ [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ if (self.scrollView.panGestureRecognizer.state != UIGestureRecognizerStateCancelled) { CGFloat top = self.scrollViewOriginalInset.top + self.mj_h; // 增長滾動區域top self.scrollView.mj_insetT = top; // 設置滾動位置 CGPoint offset = self.scrollView.contentOffset; offset.y = -top; [self.scrollView setContentOffset:offset animated:NO]; } } completion:^(BOOL finished) { [self executeRefreshingCallback]; }]; }) } }