Skip to content

Instantly share code, notes, and snippets.

@mokagio
Created May 21, 2013 22:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mokagio/5623686 to your computer and use it in GitHub Desktop.
MCSpriteLayer, core component to create sprite sheet based animation. See http://mysterycoconut.com/blog/2011/01/cag1/
#import <QuartzCore/QuartzCore.h>
@interface MCSpriteLayer : CALayer {
unsigned int sampleIndex;
}
@property (readwrite, nonatomic) unsigned int sampleIndex;
#import "MCSpriteLayer.h"
@implementation MCSpriteLayer
@synthesize sampleIndex;
+ (BOOL)needsDisplayForKey:(NSString *)key;
{
return [key isEqualToString:@"sampleIndex"];
}
- (void)display;
{
unsigned int currentSampleIndex = ((MCSpriteLayer*)[self presentationLayer]).sampleIndex;
if (!currentSampleIndex)
return;
CGSize sampleSize = self.contentsRect.size;
self.contentsRect = CGRectMake(
((currentSampleIndex - 1) % (int)(1/sampleSize.width)) * sampleSize.width,
((currentSampleIndex - 1) / (int)(1/sampleSize.width)) * sampleSize.height,
sampleSize.width, sampleSize.height
);
}
@end
@mokagio
Copy link
Author

mokagio commented May 21, 2013

I don't own this code, just took it from the Mystery Coconut Blog. All credits to them!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment