禁用CALayer隐式animation

下面的类正试图阻止CALayer更改属性时发生的任何隐式animation。

 // NoImplicitAnimations.h #import <Foundation/Foundation.h> #import <QuartzCore/QuartzCore.h> @interface NoImplicitAnimations : NSObject - (id<CAAction>) actionForLayer:(CALayer *)layer forKey:(NSString *)key; @end // NoImplicitAnimations.m #import "NoImplicitAnimations.h" @implementation NoImplicitAnimations - (id<CAAction>) actionForLayer:(CALayer *)layer forKey:(NSString *)key { return (id)[NSNull null]; } @end 

我将Objective-C中的NoImplicitAnimations.h导入Swift桥接头。

我创build一个全局常量let _noImplicitAnimations = NoImplicitAnimations()

我这样扩展CALayer类:

 extension CALayer { func noImplicitAnimations () { delegate = _noImplicitAnimations } } 

现在出现这个问题。 我创buildmyLayer后立即使用myLayer.noImplicitAnimations() 。 然而,隐含的animation仍在发生。

我在这里做错了什么?

没关系。 这实际上工作。 我在错误的CALayer上testing它。 我的错!