KKWRefreshHeaderAnimator.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // ESRefreshHeaderView.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. // Icon from http://www.iconfont.cn
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. import Foundation
  27. import QuartzCore
  28. import UIKit
  29. import ESPullToRefresh
  30. open class KKWRefreshHeaderAnimator: UIView, ESRefreshProtocol, ESRefreshAnimatorProtocol, ESRefreshImpactProtocol {
  31. open var pullToRefreshDescription = "Pull to refresh"
  32. open var releaseToRefreshDescription = "Release to refresh"
  33. open var loadingDescription = "Loading..."
  34. open var view: UIView { return self }
  35. open var insets: UIEdgeInsets = UIEdgeInsets.zero
  36. open var trigger: CGFloat = 60.0
  37. open var executeIncremental: CGFloat = 60.0
  38. open var state: ESRefreshViewState = .pullToRefresh
  39. fileprivate let imageView: UIImageView = {
  40. let imageView = UIImageView.init()
  41. if #available(iOS 8, *) {
  42. imageView.image = UIImage(named: "icon_pull_to_refresh_arrow", in: Bundle(for: ESRefreshHeaderAnimator.self), compatibleWith: nil)
  43. } else {
  44. imageView.image = UIImage(named: "icon_pull_to_refresh_arrow")
  45. }
  46. return imageView
  47. }()
  48. fileprivate let titleLabel: UILabel = {
  49. let label = UILabel.init(frame: CGRect.zero)
  50. label.font = UIFont.systemFont(ofSize: 14.0)
  51. label.textColor = UIColor.init(white: 0.625, alpha: 1.0)
  52. label.textAlignment = .left
  53. return label
  54. }()
  55. fileprivate let indicatorView: UIActivityIndicatorView = {
  56. let indicatorView = UIActivityIndicatorView.init(style: .gray)
  57. indicatorView.color = UIColor(hexString: "#5686F9")
  58. indicatorView.isHidden = true
  59. return indicatorView
  60. }()
  61. public override init(frame: CGRect) {
  62. super.init(frame: frame)
  63. titleLabel.text = pullToRefreshDescription
  64. self.addSubview(imageView)
  65. self.addSubview(titleLabel)
  66. self.addSubview(indicatorView)
  67. }
  68. public required init(coder aDecoder: NSCoder) {
  69. fatalError("init(coder:) has not been implemented")
  70. }
  71. open func refreshAnimationBegin(view: ESRefreshComponent) {
  72. indicatorView.startAnimating()
  73. indicatorView.isHidden = false
  74. imageView.isHidden = true
  75. titleLabel.text = loadingDescription
  76. imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat(M_PI))
  77. }
  78. open func refreshAnimationEnd(view: ESRefreshComponent) {
  79. indicatorView.stopAnimating()
  80. indicatorView.isHidden = true
  81. imageView.isHidden = false
  82. titleLabel.text = pullToRefreshDescription
  83. imageView.transform = CGAffineTransform.identity
  84. }
  85. open func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat) {
  86. // Do nothing
  87. }
  88. open func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState) {
  89. guard self.state != state else {
  90. return
  91. }
  92. self.state = state
  93. switch state {
  94. case .refreshing, .autoRefreshing:
  95. titleLabel.text = loadingDescription
  96. self.setNeedsLayout()
  97. break
  98. case .releaseToRefresh:
  99. titleLabel.text = releaseToRefreshDescription
  100. self.setNeedsLayout()
  101. impact()
  102. UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
  103. [weak self] in
  104. self?.imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat(M_PI))
  105. }) { (animated) in }
  106. break
  107. case .pullToRefresh:
  108. titleLabel.text = pullToRefreshDescription
  109. self.setNeedsLayout()
  110. UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
  111. [weak self] in
  112. self?.imageView.transform = CGAffineTransform.identity
  113. }) { (animated) in }
  114. break
  115. default:
  116. break
  117. }
  118. }
  119. open override func layoutSubviews() {
  120. super.layoutSubviews()
  121. let s = self.bounds.size
  122. let w = s.width
  123. let h = s.height
  124. UIView.performWithoutAnimation {
  125. titleLabel.sizeToFit()
  126. titleLabel.center = CGPoint.init(x: w / 2.0, y: h / 2.0)
  127. indicatorView.center = CGPoint.init(x: titleLabel.frame.origin.x - 16.0, y: h / 2.0)
  128. imageView.frame = CGRect.init(x: titleLabel.frame.origin.x - 28.0, y: (h - 18.0) / 2.0, width: 18.0, height: 18.0)
  129. }
  130. }
  131. }