简体   繁体   English

有没有办法禁用ios上的所有动画?

[英]Is there a way to disable all animations on ios?

Callbacks should still be called. 仍应调用回调。 I want to use it for KIF testing. 我想用它进行KIF测试。 The purpose is reducing the test time. 目的是减少测试时间。

The simplest way is to set the speed property of the window layer to a high number. 最简单的方法是将窗口层的speed属性设置为较大的数字。 What this does is control the speed of animation; 这样做是为了控制动画的速度; a value of >1 increases speed. 值> 1会提高速度。 Set a value high enough, and animations will take shorter than frame length (1/60th of a second). 设置一个足够高的值,动画将比帧长度(1/60秒)短。

This is the best option because it affects both UIKit and Core Animation animations. 这是最佳选择,因为它会同时影响UIKit和Core Animation动画。 Starting with iOS7, a lot of the system animations are actually performed with Core Animation directly. 从iOS7开始,很多系统动画实际上是直接使用Core Animation执行的。

Embellishing @Leo's answer: 装饰@Leo的答案:

If you don't need fine-grain control, setting the speed property of the keyWindow of your application will speed up all your animations in most cases. 如果您不需要细粒度控制,在大多数情况下,设置应用程序的keyWindow的speed属性将加速所有动画。 So you just need: 所以你只需要:

[UIApplication sharedApplication].keyWindow.layer.speed = 5.0;

This can be called anywhere you want. 这可以在任何你想要的地方调用。 We have it set up in in all of our tests so that animations are always fast. 我们在所有测试中都设置了它,因此动画总是很快。 (Achieve by subclassing or using beforeAll if your'e going BDD or whatever structure you want.) (通过子类化或使用beforeAll实现,如果你的BDD或你想要的任何结构。)

Also, in case you're interested, we've seen an approximate 10% speed increase, on average, when going from speed = 1.0 to speed = 5.0, where 5.0 is effectively fast enough to be almost instantaneous. 另外,如果您感兴趣的话,我们已经看到,当从速度= 1.0到速度= 5.0时,平均速度增加大约10%,其中5.0实际上足够快,几乎是瞬时的。 This obviously will vary depending on how many animations your app goes through but given how easy it is to enable, it's a no brainer for most tests. 这显然取决于您的应用程序通过多少动画,但考虑到启用它是多么容易,对于大多数测试来说,它是没有脑子的。

update: 更新:

I tried this and it seems to work. 我尝试了这个似乎工作。 Animations will "run", they just take no time at all... 动画将“运行”,他们只是不花时间......

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, "@@:@" ) ;

    ...

}

This changes the duration of every animation passed to -addAnimation:forKey: to 0.0 , and also returns nil for all calls to -actionForKey: 这会将传递给-addAnimation:forKey:的每个动画的持续时间更改为0.0 ,并且对-actionForKey的所有调用也返回nil

You can't actually just remove the animations--they may have side effects and actions tied to their completion. 你实际上不能删除动画 - 它们可能有副作用和与完成相关的动作。


Further to what @michaels suggested: 继@michaels建议:

You could try swizzling -CALayer addAnimation:forKey: to do nothing. 你可以尝试调配-Calayer addAnimation:forKey:什么都不做。 (Maybe also -CALayer actionForKey: to return null .) (也许还有-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@:@@" ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM