|
| 1 | +// |
| 2 | +// FCXRefreshBaseView.swift |
| 3 | +// FCXRefresh |
| 4 | +// |
| 5 | +// Created by 冯 传祥 on 2017/6/16. |
| 6 | +// Copyright © 2017年 冯 传祥. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +open class FCXRefreshBaseView: UIView { |
| 12 | + required public init(frame: CGRect, hangingHeight hangingH: CGFloat = 60, refreshType type: FCXRefreshViewType = .header, refreshHandler handler: @escaping (FCXRefreshBaseView) -> Void) { |
| 13 | + refreshType = type |
| 14 | + refreshHandler = handler |
| 15 | + hangingHeight = hangingH |
| 16 | + super.init(frame: frame) |
| 17 | + |
| 18 | + setupContentView() |
| 19 | + } |
| 20 | + |
| 21 | + open override func willMove(toSuperview newSuperview: UIView?) { |
| 22 | + super.willMove(toSuperview: newSuperview) |
| 23 | + |
| 24 | + removeScrollViewObservers() |
| 25 | + guard let newSuperview = newSuperview as? UIScrollView else { |
| 26 | + return |
| 27 | + } |
| 28 | + scrollView = newSuperview |
| 29 | + scrollViewOriginalEdgeInsets = newSuperview.contentInset |
| 30 | + addScrollViewObservers() |
| 31 | + } |
| 32 | + |
| 33 | + public var normalText: String? |
| 34 | + public var pullingText: String? |
| 35 | + public var loadingText: String? |
| 36 | + weak var scrollView: UIScrollView? |
| 37 | + public var pullingPercent: CGFloat = 0 { |
| 38 | + didSet { |
| 39 | + if pullingPercent != oldValue { |
| 40 | + pullingPercentHandler?(pullingPercent) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + public var pullingPercentHandler: ((_ percent: CGFloat) -> Void)? |
| 45 | + |
| 46 | + //scrollView刚开始的inset |
| 47 | + var scrollViewOriginalEdgeInsets = UIEdgeInsets.zero |
| 48 | + public var refreshHandler: ((FCXRefreshBaseView) -> Void)? |
| 49 | + //加载数据时悬挂的高度 |
| 50 | + public var hangingHeight: CGFloat = 60 |
| 51 | + public var refreshType = FCXRefreshViewType.header |
| 52 | + public var state = FCXRefreshViewState.noraml { |
| 53 | + didSet { |
| 54 | + if state != oldValue { |
| 55 | + switch state { |
| 56 | + case .noraml: |
| 57 | + fcxRefreshStateNormal() |
| 58 | + if oldValue == .loading {//之前是在刷新,更新时间 |
| 59 | + fcxRefreshBaseViewUpdateRefreshDate() |
| 60 | + } |
| 61 | + case .pulling: |
| 62 | + fcxRefreshViewStatePulling() |
| 63 | + case .loading: |
| 64 | + fcxRefreshViewStateLoading() |
| 65 | + case .noMoreData: |
| 66 | + fcxRefreshViewStateNoMoreData() |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + open func setupContentView() {} |
| 73 | + |
| 74 | + open func autoRefresh() {} |
| 75 | + |
| 76 | + open func endRefresh() { |
| 77 | + state = .noraml |
| 78 | + } |
| 79 | + |
| 80 | + //显示没有更多数据 |
| 81 | + open func showNoMoreData() {} |
| 82 | + |
| 83 | + //重置没有更多的数据(消除没有更多数据的状态) |
| 84 | + open func resetNoMoreData() {} |
| 85 | + |
| 86 | + @discardableResult |
| 87 | + public func pullingPercentHandler(handler: @escaping (CGFloat) -> Void) -> Self { |
| 88 | + pullingPercentHandler = handler |
| 89 | + return self |
| 90 | + } |
| 91 | + |
| 92 | + required public init?(coder aDecoder: NSCoder) { |
| 93 | + super.init(coder: aDecoder) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +//MARK:状态的处理 |
| 98 | +public extension FCXRefreshBaseView { |
| 99 | + /// 刷新的类型 |
| 100 | + /// |
| 101 | + /// - header: 头部下拉刷新 |
| 102 | + /// - footer: 底部收到加载更多 |
| 103 | + /// - autoFooter: 底部自动加载更多 |
| 104 | + public enum FCXRefreshViewType { |
| 105 | + case header, footer, autoFooter |
| 106 | + } |
| 107 | + |
| 108 | + /// 当前的状态 |
| 109 | + /// |
| 110 | + /// - noraml: 正常状态 |
| 111 | + /// - pulling: 正在下拉或上拉 |
| 112 | + /// - loading: 正在加载数据 |
| 113 | + /// - noMoreData: 没有更多数据 |
| 114 | + public enum FCXRefreshViewState { |
| 115 | + case noraml, pulling, loading, noMoreData |
| 116 | + } |
| 117 | + |
| 118 | + public func fcxRefreshStateNormal() {} |
| 119 | + public func fcxRefreshViewStatePulling() {} |
| 120 | + public func fcxRefreshViewStateLoading() {} |
| 121 | + public func fcxRefreshViewStateNoMoreData() {} |
| 122 | + public func fcxRefreshBaseViewUpdateRefreshDate() {} |
| 123 | +} |
| 124 | + |
| 125 | +//MARK: KVO |
| 126 | +private var FCXKVOContext = "FCXKVOContext" |
| 127 | +extension FCXRefreshBaseView { |
| 128 | + func addScrollViewObservers() { |
| 129 | + scrollView?.addObserver(self, forKeyPath: #keyPath(UIScrollView.contentOffset), options: .new, context: &FCXKVOContext) |
| 130 | + scrollView?.addObserver(self, forKeyPath: #keyPath(UIScrollView.contentSize), options: .new, context: &FCXKVOContext) |
| 131 | + scrollView?.addObserver(self, forKeyPath: #keyPath(UIScrollView.contentInset), options: .new, context: &FCXKVOContext) |
| 132 | + } |
| 133 | + |
| 134 | + func removeScrollViewObservers() { |
| 135 | + guard superview is UIScrollView else { |
| 136 | + return |
| 137 | + } |
| 138 | + superview?.removeObserver(self, forKeyPath: #keyPath(UIScrollView.contentOffset), context: &FCXKVOContext) |
| 139 | + superview?.removeObserver(self, forKeyPath: #keyPath(UIScrollView.contentSize), context: &FCXKVOContext) |
| 140 | + superview?.removeObserver(self, forKeyPath: #keyPath(UIScrollView.contentInset), context: &FCXKVOContext) |
| 141 | + } |
| 142 | + |
| 143 | + open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { |
| 144 | + guard context == &FCXKVOContext, |
| 145 | + let scrollView = scrollView else { |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + if keyPath == #keyPath(UIScrollView.contentSize) && refreshType != .header { |
| 150 | + self.frame.origin.y = max(scrollView.frame.size.height, scrollView.contentSize.height) |
| 151 | + }else if keyPath == #keyPath(UIScrollView.contentOffset) { |
| 152 | + //正在刷新 |
| 153 | + if state == .loading { |
| 154 | + return; |
| 155 | + } |
| 156 | + scrollViewContentOffsetDidChange(scrollView: scrollView) |
| 157 | + }else if keyPath == #keyPath(UIScrollView.contentInset) { |
| 158 | + if state != .loading {//不是正在加载的状态 |
| 159 | + scrollViewOriginalEdgeInsets = scrollView.contentInset |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + open func scrollViewContentOffsetDidChange(scrollView: UIScrollView) {} |
| 165 | +} |
| 166 | + |
0 commit comments