Brad Larsonの答えに加えて、(自分で作成した)カスタムレイヤーの場合、レイヤーのディクショナリを変更する代わりに委任を使用できます。actions
このアプローチはより動的であり、よりパフォーマンスが高い可能性があります。また、アニメーション化可能なすべてのキーを一覧表示しなくても、すべての暗黙的なアニメーションを無効にすることができます。
残念ながら、UIView
sをカスタムレイヤーデリゲートとして使用することはできません。これは、それぞれUIView
がすでに独自のレイヤーのデリゲートであるためです。ただし、次のような単純なヘルパークラスを使用できます。
@interface MyLayerDelegate : NSObject
@property (nonatomic, assign) BOOL disableImplicitAnimations;
@end
@implementation MyLayerDelegate
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
if (self.disableImplicitAnimations)
return (id)[NSNull null]; // disable all implicit animations
else return nil; // allow implicit animations
// you can also test specific key names; for example, to disable bounds animation:
// if ([event isEqualToString:@"bounds"]) return (id)[NSNull null];
}
@end
使用法(ビュー内):
MyLayerDelegate *delegate = [[MyLayerDelegate alloc] init];
// assign to a strong property, because CALayer's "delegate" property is weak
self.myLayerDelegate = delegate;
self.myLayer = [CALayer layer];
self.myLayer.delegate = delegate;
// ...
self.myLayerDelegate.disableImplicitAnimations = YES;
self.myLayer.position = (CGPoint){.x = 10, .y = 42}; // will not animate
// ...
self.myLayerDelegate.disableImplicitAnimations = NO;
self.myLayer.position = (CGPoint){.x = 0, .y = 0}; // will animate
ビューのカスタムサブレイヤーのデリゲートとしてビューのコントローラーを使用すると便利な場合があります。この場合、ヘルパークラスは必要ありませんactionForLayer:forKey:
。コントローラー内にメソッドを実装できます。
重要な注意:の基になるレイヤーのデリゲートを変更しようとしないでくださいUIView
(たとえば、暗黙のアニメーションを有効にするため)—悪いことが起こります:)
注:レイヤーの再描画をアニメーション化する(アニメーションを無効にしない)場合は、実際の再描画が後で発生する可能性があるため(おそらく、後で発生する可能性があるため)、[CALayer setNeedsDisplayInRect:]
呼び出しを内部に配置することは無意味です。この回答でCATransaction
説明されているように、適切なアプローチはカスタムプロパティを使用することです。