ESRefreshHeaderAnimator.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. open class ESRefreshHeaderAnimator: UIView, ESRefreshProtocol, ESRefreshAnimatorProtocol, ESRefreshImpactProtocol {
  30. open var pullToRefreshDescription = NSLocalizedString("Pull to refresh", comment: "") {
  31. didSet {
  32. if pullToRefreshDescription != oldValue {
  33. titleLabel.text = pullToRefreshDescription;
  34. }
  35. }
  36. }
  37. open var releaseToRefreshDescription = NSLocalizedString("Release to refresh", comment: "")
  38. open var loadingDescription = NSLocalizedString("Loading...", comment: "")
  39. open var view: UIView { return self }
  40. open var insets: UIEdgeInsets = UIEdgeInsets.zero
  41. open var trigger: CGFloat = 60.0
  42. open var executeIncremental: CGFloat = 60.0
  43. open var state: ESRefreshViewState = .pullToRefresh
  44. fileprivate let imageView: UIImageView = {
  45. let imageView = UIImageView.init()
  46. if /* Carthage */ let bundle = Bundle.init(identifier: "com.eggswift.ESPullToRefresh") {
  47. imageView.image = UIImage(named: "icon_pull_to_refresh_arrow", in: bundle, compatibleWith: nil)
  48. } else if /* CocoaPods */ let bundle = Bundle.init(identifier: "org.cocoapods.ESPullToRefresh") {
  49. imageView.image = UIImage(named: "ESPullToRefresh.bundle/icon_pull_to_refresh_arrow", in: bundle, compatibleWith: nil)
  50. } else /* Manual */ {
  51. imageView.image = UIImage(named: "icon_pull_to_refresh_arrow")
  52. }
  53. return imageView
  54. }()
  55. fileprivate let titleLabel: UILabel = {
  56. let label = UILabel.init(frame: CGRect.zero)
  57. label.font = UIFont.systemFont(ofSize: 14.0)
  58. label.textColor = UIColor.init(white: 0.625, alpha: 1.0)
  59. label.textAlignment = .left
  60. return label
  61. }()
  62. fileprivate let indicatorView: UIActivityIndicatorView = {
  63. let indicatorView = UIActivityIndicatorView.init(style: .gray)
  64. indicatorView.isHidden = true
  65. return indicatorView
  66. }()
  67. public override init(frame: CGRect) {
  68. super.init(frame: frame)
  69. titleLabel.text = pullToRefreshDescription
  70. self.addSubview(imageView)
  71. self.addSubview(titleLabel)
  72. self.addSubview(indicatorView)
  73. }
  74. public required init(coder aDecoder: NSCoder) {
  75. fatalError("init(coder:) has not been implemented")
  76. }
  77. open func refreshAnimationBegin(view: ESRefreshComponent) {
  78. indicatorView.startAnimating()
  79. indicatorView.isHidden = false
  80. imageView.isHidden = true
  81. titleLabel.text = loadingDescription
  82. imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
  83. }
  84. open func refreshAnimationEnd(view: ESRefreshComponent) {
  85. indicatorView.stopAnimating()
  86. indicatorView.isHidden = true
  87. imageView.isHidden = false
  88. titleLabel.text = pullToRefreshDescription
  89. imageView.transform = CGAffineTransform.identity
  90. }
  91. open func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat) {
  92. // Do nothing
  93. }
  94. open func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState) {
  95. guard self.state != state else {
  96. return
  97. }
  98. self.state = state
  99. switch state {
  100. case .refreshing, .autoRefreshing:
  101. titleLabel.text = loadingDescription
  102. self.setNeedsLayout()
  103. break
  104. case .releaseToRefresh:
  105. titleLabel.text = releaseToRefreshDescription
  106. self.setNeedsLayout()
  107. self.impact()
  108. UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
  109. [weak self] in
  110. self?.imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
  111. }) { (animated) in }
  112. break
  113. case .pullToRefresh:
  114. titleLabel.text = pullToRefreshDescription
  115. self.setNeedsLayout()
  116. UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
  117. [weak self] in
  118. self?.imageView.transform = CGAffineTransform.identity
  119. }) { (animated) in }
  120. break
  121. default:
  122. break
  123. }
  124. }
  125. open override func layoutSubviews() {
  126. super.layoutSubviews()
  127. let s = self.bounds.size
  128. let w = s.width
  129. let h = s.height
  130. UIView.performWithoutAnimation {
  131. titleLabel.sizeToFit()
  132. titleLabel.center = CGPoint.init(x: w / 2.0, y: h / 2.0)
  133. indicatorView.center = CGPoint.init(x: titleLabel.frame.origin.x - 16.0, y: h / 2.0)
  134. imageView.frame = CGRect.init(x: titleLabel.frame.origin.x - 28.0, y: (h - 18.0) / 2.0, width: 18.0, height: 18.0)
  135. }
  136. }
  137. }