YYAnimatedImageView.h 4.2 KB

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