ImageTransition.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ImageTransition.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/9/18.
  6. //
  7. // Copyright (c) 2019 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. import Foundation
  27. #if os(iOS) || os(tvOS) || os(visionOS)
  28. import UIKit
  29. /// Transition effect which will be used when an image downloaded and set by `UIImageView`
  30. /// extension API in Kingfisher. You can assign an enum value with transition duration as
  31. /// an item in `KingfisherOptionsInfo` to enable the animation transition.
  32. ///
  33. /// Apple's UIViewAnimationOptions is used under the hood.
  34. /// For custom transition, you should specified your own transition options, animations and
  35. /// completion handler as well.
  36. ///
  37. /// - none: No animation transition.
  38. /// - fade: Fade in the loaded image in a given duration.
  39. /// - flipFromLeft: Flip from left transition.
  40. /// - flipFromRight: Flip from right transition.
  41. /// - flipFromTop: Flip from top transition.
  42. /// - flipFromBottom: Flip from bottom transition.
  43. /// - custom: Custom transition.
  44. public enum ImageTransition {
  45. /// No animation transition.
  46. case none
  47. /// Fade in the loaded image in a given duration.
  48. case fade(TimeInterval)
  49. /// Flip from left transition.
  50. case flipFromLeft(TimeInterval)
  51. /// Flip from right transition.
  52. case flipFromRight(TimeInterval)
  53. /// Flip from top transition.
  54. case flipFromTop(TimeInterval)
  55. /// Flip from bottom transition.
  56. case flipFromBottom(TimeInterval)
  57. /// Custom transition defined by a general animation block.
  58. /// - duration: The time duration of this custom transition.
  59. /// - options: `UIView.AnimationOptions` should be used in the transition.
  60. /// - animations: The animation block will be applied when setting image.
  61. /// - completion: A block called when the transition animation finishes.
  62. case custom(duration: TimeInterval,
  63. options: UIView.AnimationOptions,
  64. animations: ((UIImageView, UIImage) -> Void)?,
  65. completion: ((Bool) -> Void)?)
  66. var duration: TimeInterval {
  67. switch self {
  68. case .none: return 0
  69. case .fade(let duration): return duration
  70. case .flipFromLeft(let duration): return duration
  71. case .flipFromRight(let duration): return duration
  72. case .flipFromTop(let duration): return duration
  73. case .flipFromBottom(let duration): return duration
  74. case .custom(let duration, _, _, _): return duration
  75. }
  76. }
  77. var animationOptions: UIView.AnimationOptions {
  78. switch self {
  79. case .none: return []
  80. case .fade: return .transitionCrossDissolve
  81. case .flipFromLeft: return .transitionFlipFromLeft
  82. case .flipFromRight: return .transitionFlipFromRight
  83. case .flipFromTop: return .transitionFlipFromTop
  84. case .flipFromBottom: return .transitionFlipFromBottom
  85. case .custom(_, let options, _, _): return options
  86. }
  87. }
  88. var animations: ((UIImageView, UIImage) -> Void)? {
  89. switch self {
  90. case .custom(_, _, let animations, _): return animations
  91. default: return { $0.image = $1 }
  92. }
  93. }
  94. var completion: ((Bool) -> Void)? {
  95. switch self {
  96. case .custom(_, _, _, let completion): return completion
  97. default: return nil
  98. }
  99. }
  100. }
  101. #else
  102. // Just a placeholder for compiling on macOS.
  103. public enum ImageTransition {
  104. case none
  105. /// This is a placeholder on macOS now. It is for SwiftUI (KFImage) to identify the fade option only.
  106. case fade(TimeInterval)
  107. }
  108. #endif