ImageTransition.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // ImageTransition.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/9/18.
  6. //
  7. // Copyright (c) 2016 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(OSX)
  27. // Not implemented for OSX and watchOS yet.
  28. import AppKit
  29. public enum ImageTransition {
  30. case None
  31. var duration: NSTimeInterval {
  32. return 0
  33. }
  34. }
  35. #elseif os(watchOS)
  36. import UIKit
  37. public enum ImageTransition {
  38. case None
  39. var duration: NSTimeInterval {
  40. return 0
  41. }
  42. }
  43. #else
  44. import UIKit
  45. /**
  46. Transition effect to use when an image downloaded and set by `UIImageView` extension API in Kingfisher.
  47. You can assign an enum value with transition duration as an item in `KingfisherOptionsInfo`
  48. to enable the animation transition.
  49. Apple's UIViewAnimationOptions is used under the hood.
  50. For custom transition, you should specified your own transition options, animations and
  51. comletion handler as well.
  52. - None: No animation transistion.
  53. - Fade: Fade in the loaded image.
  54. - FlipFromLeft: Flip from left transition.
  55. - FlipFromRight: Flip from right transition.
  56. - FlipFromTop: Flip from top transition.
  57. - FlipFromBottom: Flip from bottom transition.
  58. - Custom: Custom transition.
  59. */
  60. public enum ImageTransition {
  61. case None
  62. case Fade(NSTimeInterval)
  63. case FlipFromLeft(NSTimeInterval)
  64. case FlipFromRight(NSTimeInterval)
  65. case FlipFromTop(NSTimeInterval)
  66. case FlipFromBottom(NSTimeInterval)
  67. case Custom(duration: NSTimeInterval,
  68. options: UIViewAnimationOptions,
  69. animations: ((UIImageView, UIImage) -> Void)?,
  70. completion: ((Bool) -> Void)?)
  71. var duration: NSTimeInterval {
  72. switch self {
  73. case .None: return 0
  74. case .Fade(let duration): return duration
  75. case .FlipFromLeft(let duration): return duration
  76. case .FlipFromRight(let duration): return duration
  77. case .FlipFromTop(let duration): return duration
  78. case .FlipFromBottom(let duration): return duration
  79. case .Custom(let duration, _, _, _): return duration
  80. }
  81. }
  82. var animationOptions: UIViewAnimationOptions {
  83. switch self {
  84. case .None: return .TransitionNone
  85. case .Fade(_): return .TransitionCrossDissolve
  86. case .FlipFromLeft(_): return .TransitionFlipFromLeft
  87. case .FlipFromRight(_): return .TransitionFlipFromRight
  88. case .FlipFromTop(_): return .TransitionFlipFromTop
  89. case .FlipFromBottom(_): return .TransitionFlipFromBottom
  90. case .Custom(_, let options, _, _): return options
  91. }
  92. }
  93. var animations: ((UIImageView, UIImage) -> Void)? {
  94. switch self {
  95. case .Custom(_, _, let animations, _): return animations
  96. default: return {$0.image = $1}
  97. }
  98. }
  99. var completion: ((Bool) -> Void)? {
  100. switch self {
  101. case .Custom(_, _, _, let completion): return completion
  102. default: return nil
  103. }
  104. }
  105. }
  106. #endif