SDWebImageTransition.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDWebImageCompat.h"
  9. #if SD_UIKIT || SD_MAC
  10. #import "SDImageCache.h"
  11. #if SD_UIKIT
  12. typedef UIViewAnimationOptions SDWebImageAnimationOptions;
  13. #else
  14. typedef NS_OPTIONS(NSUInteger, SDWebImageAnimationOptions) {
  15. SDWebImageAnimationOptionAllowsImplicitAnimation = 1 << 0, // specify `allowsImplicitAnimation` for the `NSAnimationContext`
  16. SDWebImageAnimationOptionCurveEaseInOut = 0 << 16, // default
  17. SDWebImageAnimationOptionCurveEaseIn = 1 << 16,
  18. SDWebImageAnimationOptionCurveEaseOut = 2 << 16,
  19. SDWebImageAnimationOptionCurveLinear = 3 << 16,
  20. SDWebImageAnimationOptionTransitionNone = 0 << 20, // default
  21. SDWebImageAnimationOptionTransitionFlipFromLeft = 1 << 20,
  22. SDWebImageAnimationOptionTransitionFlipFromRight = 2 << 20,
  23. SDWebImageAnimationOptionTransitionCurlUp = 3 << 20,
  24. SDWebImageAnimationOptionTransitionCurlDown = 4 << 20,
  25. SDWebImageAnimationOptionTransitionCrossDissolve = 5 << 20,
  26. SDWebImageAnimationOptionTransitionFlipFromTop = 6 << 20,
  27. SDWebImageAnimationOptionTransitionFlipFromBottom = 7 << 20,
  28. };
  29. #endif
  30. typedef void (^SDWebImageTransitionPreparesBlock)(__kindof UIView * _Nonnull view, UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL);
  31. typedef void (^SDWebImageTransitionAnimationsBlock)(__kindof UIView * _Nonnull view, UIImage * _Nullable image);
  32. typedef void (^SDWebImageTransitionCompletionBlock)(BOOL finished);
  33. /**
  34. This class is used to provide a transition animation after the view category load image finished. Use this on `sd_imageTransition` in UIView+WebCache.h
  35. for UIKit(iOS & tvOS), we use `+[UIView transitionWithView:duration:options:animations:completion]` for transition animation.
  36. for AppKit(macOS), we use `+[NSAnimationContext runAnimationGroup:completionHandler:]` for transition animation. You can call `+[NSAnimationContext currentContext]` to grab the context during animations block.
  37. @note These transition are provided for basic usage. If you need complicated animation, consider to directly use Core Animation or use `SDWebImageAvoidAutoSetImage` and implement your own after image load finished.
  38. */
  39. @interface SDWebImageTransition : NSObject
  40. /**
  41. By default, we set the image to the view at the beginning of the animations. You can disable this and provide custom set image process
  42. */
  43. @property (nonatomic, assign) BOOL avoidAutoSetImage;
  44. /**
  45. The duration of the transition animation, measured in seconds. Defaults to 0.5.
  46. */
  47. @property (nonatomic, assign) NSTimeInterval duration;
  48. /**
  49. The timing function used for all animations within this transition animation (macOS).
  50. */
  51. @property (nonatomic, strong, nullable) CAMediaTimingFunction *timingFunction API_UNAVAILABLE(ios, tvos, watchos) API_DEPRECATED("Use SDWebImageAnimationOptions instead, or grab NSAnimationContext.currentContext and modify the timingFunction", macos(10.10, 10.10));
  52. /**
  53. A mask of options indicating how you want to perform the animations.
  54. */
  55. @property (nonatomic, assign) SDWebImageAnimationOptions animationOptions;
  56. /**
  57. A block object to be executed before the animation sequence starts.
  58. */
  59. @property (nonatomic, copy, nullable) SDWebImageTransitionPreparesBlock prepares;
  60. /**
  61. A block object that contains the changes you want to make to the specified view.
  62. */
  63. @property (nonatomic, copy, nullable) SDWebImageTransitionAnimationsBlock animations;
  64. /**
  65. A block object to be executed when the animation sequence ends.
  66. */
  67. @property (nonatomic, copy, nullable) SDWebImageTransitionCompletionBlock completion;
  68. @end
  69. /**
  70. Convenience way to create transition. Remember to specify the duration if needed.
  71. for UIKit, these transition just use the correspond `animationOptions`. By default we enable `UIViewAnimationOptionAllowUserInteraction` to allow user interaction during transition.
  72. for AppKit, these transition use Core Animation in `animations`. So your view must be layer-backed. Set `wantsLayer = YES` before you apply it.
  73. */
  74. @interface SDWebImageTransition (Conveniences)
  75. /// Fade-in transition.
  76. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *fadeTransition;
  77. /// Flip from left transition.
  78. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromLeftTransition;
  79. /// Flip from right transition.
  80. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromRightTransition;
  81. /// Flip from top transition.
  82. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromTopTransition;
  83. /// Flip from bottom transition.
  84. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromBottomTransition;
  85. /// Curl up transition.
  86. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *curlUpTransition;
  87. /// Curl down transition.
  88. @property (nonatomic, class, nonnull, readonly) SDWebImageTransition *curlDownTransition;
  89. /// Fade-in transition with duration.
  90. /// @param duration transition duration, use ease-in-out
  91. + (nonnull instancetype)fadeTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(fade(duration:));
  92. /// Flip from left transition with duration.
  93. /// @param duration transition duration, use ease-in-out
  94. + (nonnull instancetype)flipFromLeftTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(flipFromLeft(duration:));
  95. /// Flip from right transition with duration.
  96. /// @param duration transition duration, use ease-in-out
  97. + (nonnull instancetype)flipFromRightTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(flipFromRight(duration:));
  98. /// Flip from top transition with duration.
  99. /// @param duration transition duration, use ease-in-out
  100. + (nonnull instancetype)flipFromTopTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(flipFromTop(duration:));
  101. /// Flip from bottom transition with duration.
  102. /// @param duration transition duration, use ease-in-out
  103. + (nonnull instancetype)flipFromBottomTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(flipFromBottom(duration:));
  104. /// Curl up transition with duration.
  105. /// @param duration transition duration, use ease-in-out
  106. + (nonnull instancetype)curlUpTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(curlUp(duration:));
  107. /// Curl down transition with duration.
  108. /// @param duration transition duration, use ease-in-out
  109. + (nonnull instancetype)curlDownTransitionWithDuration:(NSTimeInterval)duration NS_SWIFT_NAME(curlDown(duration:));
  110. @end
  111. #endif