Skip to content

Instantly share code, notes, and snippets.

@0xced
Created August 31, 2012 13:56
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xced/89223f37e2764c283767 to your computer and use it in GitHub Desktop.
Save 0xced/89223f37e2764c283767 to your computer and use it in GitHub Desktop.
Slow animations in the iOS Simulator, also for CAAnimations
// Slow animations in the iOS Simulator, also for CAAnimations
#if TARGET_IPHONE_SIMULATOR
#import <QuartzCore/QuartzCore.h>
#import <dlfcn.h>
#import <mach-o/dyld.h>
#import <objc/runtime.h>
static CGFloat (*UIAnimationDragCoefficient)(void) = NULL;
@interface CALayer (SlowAnimations)
- (void) slow_addAnimation:(CAAnimation *)animation forKey:(NSString *)key;
@end
@implementation CALayer (SlowAnimations)
+ (void) load
{
@autoreleasepool
{
void *UIKit = dlopen([[[NSBundle bundleForClass:[UIApplication class]] executablePath] fileSystemRepresentation], RTLD_LAZY);
UIAnimationDragCoefficient = (CGFloat (*)(void))dlsym(UIKit, "UIAnimationDragCoefficient");
if (UIAnimationDragCoefficient)
{
Method addAnimationForKey = class_getInstanceMethod(self, @selector(addAnimation:forKey:));
Method slowAddAnimationForKey = class_getInstanceMethod(self, @selector(slow_addAnimation:forKey:));
method_exchangeImplementations(addAnimationForKey, slowAddAnimationForKey);
}
}
}
- (void) slow_addAnimation:(CAAnimation *)animation forKey:(NSString *)key
{
BOOL callerIsApplication = NO;
for (uint32_t i = 0; i < _dyld_image_count(); i++)
{
const struct mach_header *header = _dyld_get_image_header(i);
if (header->filetype == MH_EXECUTE)
{
Dl_info info;
if (dladdr(__builtin_return_address(0), &info) != 0)
{
callerIsApplication = (info.dli_fbase == header);
break;
}
}
}
if (callerIsApplication)
animation.duration *= UIAnimationDragCoefficient();
[self slow_addAnimation:animation forKey:key];
}
@end
#endif
@fabiankr
Copy link

The return type of UIAnimationDragCoefficient() needs to be float instead of CGFloat for it to work in the 64 bit iPhone Simulator.

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