ImageTransition.swift 4.5 KB

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