-
-
Save 0xced/89223f37e2764c283767 to your computer and use it in GitHub Desktop.
Slow animations in the iOS Simulator, also for CAAnimations
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
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The return type of
UIAnimationDragCoefficient()
needs to befloat
instead ofCGFloat
for it to work in the 64 bit iPhone Simulator.