ESRefreshProtocol.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ESRefreshProtocol.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 Foundation
  26. import UIKit
  27. public enum ESRefreshViewState {
  28. case pullToRefresh
  29. case releaseToRefresh
  30. case refreshing
  31. case autoRefreshing
  32. case noMoreData
  33. }
  34. /**
  35. * ESRefreshProtocol
  36. * Animation event handling callback protocol
  37. * You can customize the refresh or custom animation effects
  38. * Mutating is to be able to modify or enum struct variable in the method - http://swifter.tips/protocol-mutation/ by ONEVCAT
  39. */
  40. public protocol ESRefreshProtocol {
  41. /**
  42. Refresh operation begins execution method
  43. You can refresh your animation logic here, it will need to start the animation each time a refresh
  44. */
  45. mutating func refreshAnimationBegin(view: ESRefreshComponent)
  46. /**
  47. Refresh operation stop execution method
  48. Here you can reset your refresh control UI, such as a Stop UIImageView animations or some opened Timer refresh, etc., it will be executed once each time the need to end the animation
  49. */
  50. mutating func refreshAnimationEnd(view: ESRefreshComponent)
  51. /**
  52. Pulling status callback , progress is the percentage of the current offset with trigger, and avoid doing too many tasks in this process so as not to affect the fluency.
  53. */
  54. mutating func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat)
  55. mutating func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState)
  56. }
  57. public protocol ESRefreshAnimatorProtocol {
  58. // The view that called when component refresh, returns a custom view or self if 'self' is the customized views.
  59. var view: UIView {get}
  60. // Customized inset.
  61. var insets: UIEdgeInsets {set get}
  62. // Refresh event is executed threshold required y offset, set a value greater than 0.0, the default is 60.0
  63. var trigger: CGFloat {set get}
  64. // Offset y refresh event executed by this parameter you can customize the animation to perform when you refresh the view of reservations height
  65. var executeIncremental: CGFloat {set get}
  66. // Current refresh state, default is .pullToRefresh
  67. var state: ESRefreshViewState {set get}
  68. }
  69. /**
  70. * ESRefreshImpacter
  71. * Support iPhone7/iPhone7 Plus or later feedback impact
  72. * You can confirm the ESRefreshImpactProtocol
  73. */
  74. fileprivate class ESRefreshImpacter {
  75. static private var impacter: AnyObject? = {
  76. if #available(iOS 10.0, *) {
  77. if NSClassFromString("UIFeedbackGenerator") != nil {
  78. let generator = UIImpactFeedbackGenerator.init(style: .light)
  79. generator.prepare()
  80. return generator
  81. }
  82. }
  83. return nil
  84. }()
  85. static public func impact() -> Void {
  86. if #available(iOS 10.0, *) {
  87. if let impacter = impacter as? UIImpactFeedbackGenerator {
  88. impacter.impactOccurred()
  89. }
  90. }
  91. }
  92. }
  93. public protocol ESRefreshImpactProtocol {}
  94. public extension ESRefreshImpactProtocol {
  95. func impact() -> Void {
  96. ESRefreshImpacter.impact()
  97. }
  98. }