ImageTransition.swift 4.2 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. import AppKit
  29. /// Image transition is not supported on macOS.
  30. public enum ImageTransition {
  31. case none
  32. var duration: TimeInterval {
  33. return 0
  34. }
  35. }
  36. #elseif os(watchOS)
  37. import UIKit
  38. /// Image transition is not supported on watchOS.
  39. public enum ImageTransition {
  40. case none
  41. var duration: TimeInterval {
  42. return 0
  43. }
  44. }
  45. #else
  46. import UIKit
  47. /**
  48. Transition effect which will be used when an image downloaded and set by `UIImageView` extension API in Kingfisher.
  49. You can assign an enum value with transition duration as an item in `KingfisherOptionsInfo`
  50. to enable the animation transition.
  51. Apple's UIViewAnimationOptions is used under the hood.
  52. For custom transition, you should specified your own transition options, animations and
  53. completion handler as well.
  54. */
  55. public enum ImageTransition {
  56. /// No animation transition.
  57. case none
  58. /// Fade in the loaded image.
  59. case fade(TimeInterval)
  60. /// Flip from left transition.
  61. case flipFromLeft(TimeInterval)
  62. /// Flip from right transition.
  63. case flipFromRight(TimeInterval)
  64. /// Flip from top transition.
  65. case flipFromTop(TimeInterval)
  66. /// Flip from bottom transition.
  67. case flipFromBottom(TimeInterval)
  68. /// Custom transition.
  69. case custom(duration: TimeInterval,
  70. options: UIView.AnimationOptions,
  71. animations: ((UIImageView, UIImage) -> Void)?,
  72. completion: ((Bool) -> Void)?)
  73. var duration: TimeInterval {
  74. switch self {
  75. case .none: return 0
  76. case .fade(let duration): return duration
  77. case .flipFromLeft(let duration): return duration
  78. case .flipFromRight(let duration): return duration
  79. case .flipFromTop(let duration): return duration
  80. case .flipFromBottom(let duration): return duration
  81. case .custom(let duration, _, _, _): return duration
  82. }
  83. }
  84. var animationOptions: UIView.AnimationOptions {
  85. switch self {
  86. case .none: return []
  87. case .fade(_): return .transitionCrossDissolve
  88. case .flipFromLeft(_): return .transitionFlipFromLeft
  89. case .flipFromRight(_): return .transitionFlipFromRight
  90. case .flipFromTop(_): return .transitionFlipFromTop
  91. case .flipFromBottom(_): return .transitionFlipFromBottom
  92. case .custom(_, let options, _, _): return options
  93. }
  94. }
  95. var animations: ((UIImageView, UIImage) -> Void)? {
  96. switch self {
  97. case .custom(_, _, let animations, _): return animations
  98. default: return { $0.image = $1 }
  99. }
  100. }
  101. var completion: ((Bool) -> Void)? {
  102. switch self {
  103. case .custom(_, _, _, let completion): return completion
  104. default: return nil
  105. }
  106. }
  107. }
  108. #endif