Posted By

sidneydekoning on 04/30/14


Tagged

object images sprite Objective-c sheet texture spritesheet mob blitter


Versions (?)

Simple Spritesheet


 / Published in: Objective C
 

Very simple use of an non-animating spritesheet for iOS

  1. -(void) addSpriteImage:(UIImage *) image
  2. withContentRect:(CGRect) rect
  3. toLayer:(CALayer *) layer
  4. {
  5. //set image
  6. layer.contents = (__bridge id)image.CGImage;
  7. //scale contents to fit
  8. layer.contentsGravity = kCAGravityResizeAspect;
  9. //set contentRect to appropriate place in image
  10. layer.contentsRect = rect;
  11. }
  12.  
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16.  
  17. UIImage *image = [UIImage imageNamed:@"spritesheet.png"];
  18.  
  19. [self addSpriteImage:image withContentRect:CGRectMake(0, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
  20.  
  21. [self addSpriteImage:image withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
  22.  
  23. [self addSpriteImage:image withContentRect:CGRectMake(0, 0.5, 0.5, 0.5) toLayer:self.someUIView.layer];
  24.  
  25. [self addSpriteImage:image withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
  26.  
  27. }

Report this snippet  

You need to login to post a comment.