Created
May 21, 2013 22:08
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <QuartzCore/QuartzCore.h> | |
@interface MCSpriteLayer : CALayer { | |
unsigned int sampleIndex; | |
} | |
@property (readwrite, nonatomic) unsigned int sampleIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't own this code, just took it from the Mystery Coconut Blog. All credits to them!