YYSpriteSheetImage.h 3.3 KB

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