Skip to content

Instantly share code, notes, and snippets.

@adamgit
Created October 5, 2012 14:11
Show Gist options
  • Save adamgit/3839993 to your computer and use it in GitHub Desktop.
Save adamgit/3839993 to your computer and use it in GitHub Desktop.
VCLoadApplication method swizzle
@implementation UIView (swizzled)
-(void) swizzledSetNeedsDisplay
{
//if( _currentFrame != _lastDrawnFrame )
{
//[super setNeedsDisplay];
}
}
@end
@implementation VCLoadApplication
/** Mike Ash's suggested class swizzling function */
void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void)viewDidAppear:(BOOL)animated
{
/** patch the sprite's to change their setNeedsDisplay */
int numClasses = objc_getClassList(NULL, 0);
Class *classes = NULL;
classes = malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
NSMutableArray *result = [NSMutableArray array];
for (NSInteger i = 0; i < numClasses; i++)
{
Class nextClass = classes[i];
NSString* className = [nextClass description];
if( [className hasSuffix:@"SpriteBase"] )
{
// swizzle it
NSLog(@"[%@] Swizzling class: %@", [self class], className );
Swizzle( nextClass, @selector(setNeedsDisplay), <#SEL new#>)
}
}
free(classes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment