ESRefreshFooterAnimator.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // ESRefreshFooterAnimator.swift
  3. //
  4. // Created by egg swift on 16/4/7.
  5. // Copyright (c) 2013-2016 ESPullToRefresh (https://github.com/eggswift/pull-to-refresh)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import UIKit
  26. open class ESRefreshFooterAnimator: UIView, ESRefreshProtocol, ESRefreshAnimatorProtocol {
  27. open var loadingMoreDescription: String = NSLocalizedString("Loading more", comment: "")
  28. open var noMoreDataDescription: String = NSLocalizedString("No more data", comment: "")
  29. open var loadingDescription: String = NSLocalizedString("Loading...", comment: "")
  30. open var view: UIView { return self }
  31. open var duration: TimeInterval = 0.3
  32. open var insets: UIEdgeInsets = UIEdgeInsets.zero
  33. open var trigger: CGFloat = 42.0
  34. open var executeIncremental: CGFloat = 42.0
  35. open var state: ESRefreshViewState = .pullToRefresh
  36. fileprivate let titleLabel: UILabel = {
  37. let label = UILabel.init(frame: CGRect.zero)
  38. label.font = UIFont.systemFont(ofSize: 14.0)
  39. label.textColor = UIColor.init(white: 160.0 / 255.0, alpha: 1.0)
  40. label.textAlignment = .center
  41. return label
  42. }()
  43. fileprivate let indicatorView: UIActivityIndicatorView = {
  44. let indicatorView = UIActivityIndicatorView.init(style: .gray)
  45. indicatorView.isHidden = true
  46. return indicatorView
  47. }()
  48. public override init(frame: CGRect) {
  49. super.init(frame: frame)
  50. titleLabel.text = loadingMoreDescription
  51. addSubview(titleLabel)
  52. addSubview(indicatorView)
  53. }
  54. public required init(coder aDecoder: NSCoder) {
  55. fatalError("init(coder:) has not been implemented")
  56. }
  57. open func refreshAnimationBegin(view: ESRefreshComponent) {
  58. indicatorView.startAnimating()
  59. titleLabel.text = loadingDescription
  60. indicatorView.isHidden = false
  61. }
  62. open func refreshAnimationEnd(view: ESRefreshComponent) {
  63. indicatorView.stopAnimating()
  64. titleLabel.text = loadingMoreDescription
  65. indicatorView.isHidden = true
  66. }
  67. open func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat) {
  68. // do nothing
  69. }
  70. open func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState) {
  71. guard self.state != state else {
  72. return
  73. }
  74. self.state = state
  75. switch state {
  76. case .refreshing, .autoRefreshing :
  77. titleLabel.text = loadingDescription
  78. break
  79. case .noMoreData:
  80. titleLabel.text = noMoreDataDescription
  81. break
  82. case .pullToRefresh:
  83. titleLabel.text = loadingMoreDescription
  84. break
  85. default:
  86. break
  87. }
  88. self.setNeedsLayout()
  89. }
  90. open override func layoutSubviews() {
  91. super.layoutSubviews()
  92. let s = self.bounds.size
  93. let w = s.width
  94. let h = s.height
  95. titleLabel.sizeToFit()
  96. titleLabel.center = CGPoint.init(x: w / 2.0, y: h / 2.0 - 5.0)
  97. indicatorView.center = CGPoint.init(x: titleLabel.frame.origin.x - 18.0, y: titleLabel.center.y)
  98. }
  99. }