Created
May 22, 2013 17:25
-
-
Save frijole/5629307 to your computer and use it in GitHub Desktop.
UIImageView subclass method for setting up and animating an image sequence
This file contains hidden or 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
- (void)setFramesWithFirstFrame:(NSString *)fileName; | |
{ | |
// DLog(@"firstFrame: %@",fileName); | |
[self setImage:[UIImage imageNamed:fileName]]; | |
// split the `fileName` into a base and number. then incrememnt the number until imageNamed returns nil. | |
// check for numbers in the filename | |
NSRange pngRange = [fileName rangeOfString:@".png"]; | |
NSRange numberRange; | |
// if we don't find any numbers, bail out. | |
if ( pngRange.location == NSNotFound ) { | |
DLog(@"No number found. Not setting frames."); | |
return; | |
} else { | |
numberRange = NSMakeRange(pngRange.location-2, 2); | |
} | |
// grab the numbers we found | |
NSString *tmpNumberString = [fileName substringWithRange:numberRange]; | |
NSNumberFormatter *tmpNumberFormatter = [[NSNumberFormatter alloc] init]; | |
[tmpNumberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; | |
NSNumber *tmpNumber = [tmpNumberFormatter numberFromString:tmpNumberString]; | |
// check the result | |
if ( tmpNumber == nil ) { | |
DLog(@"Number conversion failed. Not setting frames."); | |
return; | |
} | |
// now we have: | |
// - fileName - @"foo00.png" | |
// - tmpNumber - number of first frame | |
// - tmpNumberFormatter - we can re-use this to format our frame numbers | |
// format the number formatter | |
[tmpNumberFormatter setMinimumIntegerDigits:2]; | |
[tmpNumberFormatter setMaximumIntegerDigits:2]; | |
// get the base filename to iterate on | |
NSString *tmpBaseFilename = [fileName substringToIndex:numberRange.location]; | |
// a place to put the frames | |
NSMutableArray *tmpFrames = [NSMutableArray array]; | |
// keep track of if we should keep looking | |
BOOL keepLooking = YES; | |
// start looking for frames! | |
while ( keepLooking ) { | |
// prepare a frame string | |
NSString *tmpFrameString = [tmpNumberFormatter stringFromNumber:tmpNumber]; | |
// incrememnt the number | |
tmpNumber = [NSNumber numberWithInt:[tmpNumber integerValue]+1]; | |
// what filename are we looking for? | |
NSString *tmpFilenameString = [tmpBaseFilename stringByAppendingString:tmpFrameString]; | |
// look for a frame! | |
UIImage *tmpImage = [UIImage imageNamed:tmpFilenameString]; | |
if ( tmpImage ) { | |
[tmpFrames addObject:tmpImage]; | |
} else { | |
// DLog(@"image not found (%@), bailing out.",tmpFilenameString); | |
keepLooking = NO; | |
} | |
} | |
// check for any frames | |
if ( tmpFrames.count > 0 ) { | |
[self setAnimationImages:tmpFrames]; | |
[self startAnimating]; | |
} else { | |
DLog(@"no frames found after processing, none set."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment