You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.6 KiB

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