YYSpriteSheetImage.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // YYSpriteImage.h
  3. // YYImage <https://github.com/ibireme/YYImage>
  4. //
  5. // Created by ibireme on 15/4/21.
  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. #if __has_include(<YYImage/YYImage.h>)
  13. #import <YYImage/YYAnimatedImageView.h>
  14. #elif __has_include(<YYWebImage/YYImage.h>)
  15. #import <YYWebImage/YYAnimatedImageView.h>
  16. #else
  17. #import "YYAnimatedImageView.h"
  18. #endif
  19. NS_ASSUME_NONNULL_BEGIN
  20. /**
  21. An image to display sprite sheet animation.
  22. @discussion It is a fully compatible `UIImage` subclass.
  23. The animation can be played by YYAnimatedImageView.
  24. Sample Code:
  25. // 8 * 12 sprites in a single sheet image
  26. UIImage *spriteSheet = [UIImage imageNamed:@"sprite-sheet"];
  27. NSMutableArray *contentRects = [NSMutableArray new];
  28. NSMutableArray *durations = [NSMutableArray new];
  29. for (int j = 0; j < 12; j++) {
  30. for (int i = 0; i < 8; i++) {
  31. CGRect rect;
  32. rect.size = CGSizeMake(img.size.width / 8, img.size.height / 12);
  33. rect.origin.x = img.size.width / 8 * i;
  34. rect.origin.y = img.size.height / 12 * j;
  35. [contentRects addObject:[NSValue valueWithCGRect:rect]];
  36. [durations addObject:@(1 / 60.0)];
  37. }
  38. }
  39. YYSpriteSheetImage *sprite;
  40. sprite = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:img
  41. contentRects:contentRects
  42. frameDurations:durations
  43. loopCount:0];
  44. YYAnimatedImageView *imgView = [YYAnimatedImageView new];
  45. imgView.size = CGSizeMake(img.size.width / 8, img.size.height / 12);
  46. imgView.image = sprite;
  47. @discussion It can also be used to display single frame in sprite sheet image.
  48. Sample Code:
  49. YYSpriteSheetImage *sheet = ...;
  50. UIImageView *imageView = ...;
  51. imageView.image = sheet;
  52. imageView.layer.contentsRect = [sheet contentsRectForCALayerAtIndex:6];
  53. */
  54. @interface YYSpriteSheetImage : UIImage <YYAnimatedImage>
  55. /**
  56. Creates and returns an image object.
  57. @param image The sprite sheet image (contains all frames).
  58. @param contentRects The sprite sheet image frame rects in the image coordinates.
  59. The rectangle should not outside the image's bounds. The objects in this array
  60. should be created with [NSValue valueWithCGRect:].
  61. @param frameDurations The sprite sheet image frame's durations in seconds.
  62. The objects in this array should be NSNumber.
  63. @param loopCount Animation loop count, 0 means infinite looping.
  64. @return An image object, or nil if an error occurs.
  65. */
  66. - (nullable instancetype)initWithSpriteSheetImage:(UIImage *)image
  67. contentRects:(NSArray<NSValue *> *)contentRects
  68. frameDurations:(NSArray<NSNumber *> *)frameDurations
  69. loopCount:(NSUInteger)loopCount;
  70. @property (nonatomic, readonly) NSArray<NSValue *> *contentRects;
  71. @property (nonatomic, readonly) NSArray<NSValue *> *frameDurations;
  72. @property (nonatomic, readonly) NSUInteger loopCount;
  73. /**
  74. Get the contents rect for CALayer.
  75. See "contentsRect" property in CALayer for more information.
  76. @param index Index of frame.
  77. @return Contents Rect.
  78. */
  79. - (CGRect)contentsRectForCALayerAtIndex:(NSUInteger)index;
  80. @end
  81. NS_ASSUME_NONNULL_END