ImageTransition.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // ImageTransition.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/9/18.
  6. //
  7. // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(macOS)
  27. // Not implemented for macOS and watchOS yet.
  28. /// Image transition is not supported on macOS.
  29. public enum ImageTransition {
  30. case none
  31. var duration: TimeInterval {
  32. return 0
  33. }
  34. }
  35. #elseif os(watchOS)
  36. /// Image transition is not supported on watchOS.
  37. public enum ImageTransition {
  38. case none
  39. var duration: TimeInterval {
  40. return 0
  41. }
  42. }
  43. #else
  44. import UIKit
  45. /// Transition effect which will be used when an image downloaded and set by `UIImageView` extension API in Kingfisher.
  46. /// You can assign an enum value with transition duration as an item in `KingfisherOptionsInfo`
  47. /// to enable the animation transition.
  48. ///
  49. /// Apple's UIViewAnimationOptions is used under the hood.
  50. /// For custom transition, you should specified your own transition options, animations and
  51. /// completion handler as well.
  52. ///
  53. /// - none: No animation transition.
  54. /// - fade: Fade in the loaded image in a given duration.
  55. /// - flipFromLeft: Flip from left transition.
  56. /// - flipFromRight: Flip from right transition.
  57. /// - flipFromTop: Flip from top transition.
  58. /// - flipFromBottom: Flip from bottom transition.
  59. /// - customduration: Custom transition.
  60. public enum ImageTransition {
  61. /// No animation transition.
  62. case none
  63. /// Fade in the loaded image in a given duration.
  64. case fade(TimeInterval)
  65. /// Flip from left transition.
  66. case flipFromLeft(TimeInterval)
  67. /// Flip from right transition.
  68. case flipFromRight(TimeInterval)
  69. /// Flip from top transition.
  70. case flipFromTop(TimeInterval)
  71. /// Flip from bottom transition.
  72. case flipFromBottom(TimeInterval)
  73. /// Custom transition defined by a general animation block.
  74. /// - duration: The time duration of this custom transition.
  75. /// - options: `UIView.AnimationOptions` should be used in the transition.
  76. /// - animations: The animation block will be applied when setting image.
  77. /// - completion: A block called when the transition animation finishes.
  78. case custom(duration: TimeInterval,
  79. options: UIView.AnimationOptions,
  80. animations: ((UIImageView, UIImage) -> Void)?,
  81. completion: ((Bool) -> Void)?)
  82. var duration: TimeInterval {
  83. switch self {
  84. case .none: return 0
  85. case .fade(let duration): return duration
  86. case .flipFromLeft(let duration): return duration
  87. case .flipFromRight(let duration): return duration
  88. case .flipFromTop(let duration): return duration
  89. case .flipFromBottom(let duration): return duration
  90. case .custom(let duration, _, _, _): return duration
  91. }
  92. }
  93. var animationOptions: UIView.AnimationOptions {
  94. switch self {
  95. case .none: return []
  96. case .fade: return .transitionCrossDissolve
  97. case .flipFromLeft: return .transitionFlipFromLeft
  98. case .flipFromRight: return .transitionFlipFromRight
  99. case .flipFromTop: return .transitionFlipFromTop
  100. case .flipFromBottom: return .transitionFlipFromBottom
  101. case .custom(_, let options, _, _): return options
  102. }
  103. }
  104. var animations: ((UIImageView, UIImage) -> Void)? {
  105. switch self {
  106. case .custom(_, _, let animations, _): return animations
  107. default: return { $0.image = $1 }
  108. }
  109. }
  110. var completion: ((Bool) -> Void)? {
  111. switch self {
  112. case .custom(_, _, _, let completion): return completion
  113. default: return nil
  114. }
  115. }
  116. }
  117. #endif