YYAnimatedImageView.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // YYAnimatedImageView.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/19.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. An image view for displaying animated image.
  15. @discussion It is a fully compatible `UIImageView` subclass.
  16. If the `image` or `highlightedImage` property adopt to the `YYAnimatedImage` protocol,
  17. then it can be used to play the multi-frame animation. The animation can also be
  18. controlled with the UIImageView methods `-startAnimating`, `-stopAnimating` and `-isAnimating`.
  19. This view request the frame data just in time. When the device has enough free memory,
  20. this view may cache some or all future frames in an inner buffer for lower CPU cost.
  21. Buffer size is dynamically adjusted based on the current state of the device memory.
  22. Sample Code:
  23. // ani@3x.gif
  24. YYImage *image = [YYImage imageNamed:@"ani"];
  25. YYAnimatedImageView *imageView = [YYAnimatedImageView alloc] initWithImage:image];
  26. [view addSubView:imageView];
  27. */
  28. @interface YYAnimatedImageView : UIImageView
  29. /**
  30. If the image has more than one frame, set this value to `YES` will automatically
  31. play/stop the animation when the view become visible/invisible.
  32. The default value is `YES`.
  33. */
  34. @property (nonatomic) BOOL autoPlayAnimatedImage;
  35. /**
  36. Index of the currently displayed frame (index from 0).
  37. Set a new value to this property will cause to display the new frame immediately.
  38. If the new value is invalid, this method has no effect.
  39. You can add an observer to this property to observe the playing status.
  40. */
  41. @property (nonatomic) NSUInteger currentAnimatedImageIndex;
  42. /**
  43. Whether the image view is playing animation currently.
  44. You can add an observer to this property to observe the playing status.
  45. */
  46. @property (nonatomic, readonly) BOOL currentIsPlayingAnimation;
  47. /**
  48. The animation timer's runloop mode, default is `NSRunLoopCommonModes`.
  49. Set this property to `NSDefaultRunLoopMode` will make the animation pause during
  50. UIScrollView scrolling.
  51. */
  52. @property (nonatomic, copy) NSString *runloopMode;
  53. /**
  54. The max size (in bytes) for inner frame buffer size, default is 0 (dynamically).
  55. When the device has enough free memory, this view will request and decode some or
  56. all future frame image into an inner buffer. If this property's value is 0, then
  57. the max buffer size will be dynamically adjusted based on the current state of
  58. the device free memory. Otherwise, the buffer size will be limited by this value.
  59. When receive memory warning or app enter background, the buffer will be released
  60. immediately, and may grow back at the right time.
  61. */
  62. @property (nonatomic) NSUInteger maxBufferSize;
  63. @end
  64. /**
  65. The YYAnimatedImage protocol declares the required methods for animated image
  66. display with YYAnimatedImageView.
  67. Subclass a UIImage and implement this protocol, so that instances of that class
  68. can be set to YYAnimatedImageView.image or YYAnimatedImageView.highlightedImage
  69. to display animation.
  70. See `YYImage` and `YYFrameImage` for example.
  71. */
  72. @protocol YYAnimatedImage <NSObject>
  73. @required
  74. /// Total animated frame count.
  75. /// If the frame count is less than 1, then the methods below will be ignored.
  76. - (NSUInteger)animatedImageFrameCount;
  77. /// Animation loop count, 0 means infinite looping.
  78. - (NSUInteger)animatedImageLoopCount;
  79. /// Bytes per frame (in memory). It may used to optimize memory buffer size.
  80. - (NSUInteger)animatedImageBytesPerFrame;
  81. /// Returns the frame image from a specified index.
  82. /// This method may be called on background thread.
  83. /// @param index Frame index (zero based).
  84. - (nullable UIImage *)animatedImageFrameAtIndex:(NSUInteger)index;
  85. /// Returns the frames's duration from a specified index.
  86. /// @param index Frame index (zero based).
  87. - (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index;
  88. @optional
  89. /// A rectangle in image coordinates defining the subrectangle of the image that
  90. /// will be displayed. The rectangle should not outside the image's bounds.
  91. /// It may used to display sprite animation with a single image (sprite sheet).
  92. - (CGRect)animatedImageContentsRectAtIndex:(NSUInteger)index;
  93. @end
  94. NS_ASSUME_NONNULL_END