更新:
我试过这个,它似乎工作。动画会“运行”,它们根本不需要时间......
static void (*__original_CALayerAddAnimationForKey)( CALayer *, SEL, CAAnimation *, NSString * ) ;
static void CALayerAddAnimationForKey( CALayer * self, SEL _cmd, CAAnimation * anim, NSString * key )
{
anim.duration = 0.0 ;
(*__original_CALayerAddAnimationForKey)( self, _cmd, anim, key ) ;
}
static id<CAAction> CALayerActionForKey( CALayer * self, SEL _cmd, NSString * key )
{
return nil ;
}
int main(...)
{
// head patch -[CALayer addAnimation:forKey:] to set all animation durations to 0.0
__original_CALayerAddAnimationForKey = (void(*)(CALayer *, SEL, CAAnimation *, NSString*))class_getMethodImplementation( [ CALayer class ], @selector( addAnimation:forKey: ) ) ;
assert( __original_CALayerAddAnimationForKey ) ;
class_replaceMethod( [ CALayer class ], @selector( addAnimation:forKey: ), (IMP)CALayerAddAnimationForKey, "v@:@@" ) ;
// replace -[CALayer actionForKey:] with a function that always returns nil (no action)
class_replaceMethod( [ CALayer class ], @selector( actionForKey: ), (IMP)CALayerActionForKey, "@@:@" ) ;
...
}
这会将传递给 -addAnimation:forKey: 的每个动画的持续时间更改为 0.0,并且对于所有对 -actionForKey: 的调用也返回 nil em>
您实际上不能只删除动画——它们可能具有与完成相关的副作用和操作。
进一步了解@michaels 的建议:
你可以尝试调配 -CALayer addAnimation:forKey: 什么都不做。 (也许还有 -CALayer actionForKey: 来返回 null。)
#import <objc/runtime.h>
static void CALayerAddAnimationForKey( CALayer * self, SEL _cmd, NSString * key ) { }
...
class_replaceMethod( [ CALayer class ],
@selector( addAnimation:forKey: ),
(IMP)CALayerAddAnimationForKey,
"v@:@@" ) ;