Skip to content

Instantly share code, notes, and snippets.

@kingnight
Created October 27, 2016 10:34
Show Gist options
  • Save kingnight/67fac50155e53664789ce3e922ef77b5 to your computer and use it in GitHub Desktop.
Save kingnight/67fac50155e53664789ce3e922ef77b5 to your computer and use it in GitHub Desktop.
- (void)addSpriteImage:(UIImage *)image
withContentRect:(CGRect)rect
toLayer:(CALayer *)layer
{
//set image
layer.contents = (__bridge id)image.CGImage;
//scale contents to fit
layer.contentsGravity = kCAGravityResizeAspect;
//set contentsRect
layer.contentsRect = rect;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//load sprite sheet
UIImage *image = [UIImage imageNamed:@"Sprites.png"];
//set igloo sprite
[self addSpriteImage:image
withContentRect:CGRectMake(0, 0, 0.5, 0.5)
toLayer:self.iglooView.layer];
//set cone sprite
[self addSpriteImage:image
withContentRect:CGRectMake(0.5, 0, 0.5, 0.5)
toLayer:self.coneView.layer];
//set anchor sprite
[self addSpriteImage:image
withContentRect:CGRectMake(0, 0.5, 0.5, 0.5)
toLayer:self.anchorView.layer];
//set spaceship sprite
[self addSpriteImage:image
withContentRect:CGRectMake(0.5, 0.5, 0.5, 0.5)
toLayer:self.shipView.layer];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment