我有一个层,在它的-drawInContext:方法中有一些复杂的绘图代码。我正在尝试最小化我需要做的绘图量,所以我使用-setNeedsDisplayInRect:来只更新修改过的部分。这工作得很好。然而,当图形系统更新我的图层时,它使用交叉淡入淡出从旧图像过渡到新图像。我希望它能立即切换。
我尝试过使用CATransaction关闭操作并将持续时间设置为零,但都不起作用。下面是我使用的代码:
[CATransaction begin];
[CATransaction setDisableActions: YES];
[self setNeedsDisplayInRect: rect];
[CATransaction commit];
在CATransaction上有没有不同的方法我应该用来代替(我也尝试了-setValue:forKey: with kCATransactionDisableActions,同样的结果)。
发布于 2011-03-31 00:11:21
另外:
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
//foo
[CATransaction commit];
发布于 2013-04-19 10:32:14
除了Brad Larson's answer:用于自定义图层(由您创建) you can use delegation,而不是修改图层的actions
字典。这种方法更具动态性,性能也可能更好。它允许禁用所有隐式动画,而不必列出所有可设置动画的关键点。
不幸的是,不可能将UIView
用作自定义的层委托,因为每个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
底层的代理(例如启用隐式动画)--不好的事情会发生:)
注意:如果你想为层的重绘设置动画(而不是禁用动画),那么在CATransaction
中放入[CALayer setNeedsDisplayInRect:]
调用是没有用的,因为实际的重绘可能(而且可能会)在以后发生。最好的方法是使用自定义属性,如in this answer所述。
发布于 2013-05-21 18:48:40
实际上,我没有找到任何正确的答案。为我解决这个问题的方法是:
- (id<CAAction>)actionForKey:(NSString *)event {
return nil;
}
然后,您可以使用其中的任何逻辑来禁用特定的动画,但由于我想将它们全部删除,所以我返回了nil。
https://stackoverflow.com/questions/2244147
复制相似问题