ESRefreshHeaderAnimator.swift 6.4 KB

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