YYSpriteSheetImage.h 3.4 KB

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