在 - [CALayer setNeedsDisplayInRect:]中禁用隐式动画

问题描述 投票:128回答:14

我的-drawInContext:方法中有一个包含一些复杂绘图代码的图层。我正在尝试最小化我需要做的绘图量,所以我使用-setNeedsDisplayInRect:仅更新已更改的部分。这是非常好的工作。但是,当图形系统更新我的图层时,它会使用交叉淡入淡出从旧图像过渡到新图像。我希望它能立即切换。

我已经尝试使用CATransaction来关闭操作并将持续时间设置为零,并且都不起作用。这是我正在使用的代码:

[CATransaction begin];
[CATransaction setDisableActions: YES];
[self setNeedsDisplayInRect: rect];
[CATransaction commit];

我应该使用不同的CATransaction方法(我也尝试过-setValue:forKey:使用kCATransactionDisableActions,结果相同)。

iphone ios core-animation calayer
14个回答
167
投票

您可以通过在图层上设置操作字典来将[NSNull null]作为相应键的动画返回。例如,我用

NSDictionary *newActions = @{
    @"onOrderIn": [NSNull null],
    @"onOrderOut": [NSNull null],
    @"sublayers": [NSNull null],
    @"contents": [NSNull null],
    @"bounds": [NSNull null]
};

layer.actions = newActions;

在我的一个图层中插入或更改子图层时禁用淡入/淡出动画,以及图层大小和内容的更改。我相信contents键是您正在寻找的键,以防止更新绘图上的交叉淡入淡出。


Swift版本:

let newActions = [
        "onOrderIn": NSNull(),
        "onOrderOut": NSNull(),
        "sublayers": NSNull(),
        "contents": NSNull(),
        "bounds": NSNull(),
    ]

2
投票

将其添加到您正在实现-drawRect()方法的自定义类中。对代码进行更改以满足您的需求,因为“不透明度”可以阻止交叉渐变动画。

-(id<CAAction>) actionForLayer:(CALayer *)layer forKey:(NSString *)key
{
    NSLog(@"key: %@", key);
    if([key isEqualToString:@"opacity"])
    {
        return (id<CAAction>)[NSNull null];
    }

    return [super actionForLayer:layer forKey:key];
}

1
投票

已更新为swift并且仅禁用iOS中的一个隐式属性动画而非MacOS

// Disable the implicit animation for changes to position
override open class func defaultAction(forKey event: String) -> CAAction? {
    if event == #keyPath(position) {
        return NSNull()
    }
    return super.defaultAction(forKey: event)
}

0
投票

从iOS 7开始,有一种方便的方法可以做到这一点:

[UIView performWithoutAnimation:^{
    // apply changes
}];

0
投票

要在更改CATextLayer的字符串属性时禁用恼人(模糊)动画,可以执行以下操作:

class CANullAction: CAAction {
    private static let CA_ANIMATION_CONTENTS = "contents"

    @objc
    func runActionForKey(event: String, object anObject: AnyObject, arguments dict: [NSObject : AnyObject]?) {
        // Do nothing.
    }
}

然后像这样使用它(不要忘记正确设置CATextLayer,例如正确的字体等):

caTextLayer.actions = [CANullAction.CA_ANIMATION_CONTENTS: CANullAction()]

您可以在此处查看我完整的CATextLayer设置:

private let systemFont16 = UIFont.systemFontOfSize(16.0)

caTextLayer = CATextLayer()
caTextLayer.foregroundColor = UIColor.blackColor().CGColor
caTextLayer.font = CGFontCreateWithFontName(systemFont16.fontName)
caTextLayer.fontSize = systemFont16.pointSize
caTextLayer.alignmentMode = kCAAlignmentCenter
caTextLayer.drawsAsynchronously = false
caTextLayer.actions = [CANullAction.CA_ANIMATION_CONTENTS: CANullAction()]
caTextLayer.contentsScale = UIScreen.mainScreen().scale
caTextLayer.frame = CGRectMake(playbackTimeImage.layer.bounds.origin.x, ((playbackTimeImage.layer.bounds.height - playbackTimeLayer.fontSize) / 2), playbackTimeImage.layer.bounds.width, playbackTimeLayer.fontSize * 1.2)

uiImageTarget.layer.addSublayer(caTextLayer)
caTextLayer.string = "The text you want to display"

现在你可以根据需要更新caTextLayer.string =)

灵感来自thisthis回答。


0
投票

尝试这个。

let layer = CALayer()
layer.delegate = hoo // Same lifecycle UIView instance.

警告

如果设置UITableView实例的委托,有时会发生崩溃。(可能是递归调用scrollview的hittest。)


0
投票

如果你需要一个非常快速(但不可否认的hacky)修复它可能值得做(Swift):

let layer = CALayer()

// set other properties
// ...

layer.speed = 999

90
投票

也:

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

//foo

[CATransaction commit];

30
投票

更改图层的属性时,CA通常会创建一个隐式事务对象来为更改设置动画。如果您不想为更改设置动画,可以通过创建显式事务并将其kCATransactionDisableActions属性设置为true来禁用隐式动画。

Objective-C的

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
// change properties here without animation
[CATransaction commit];

迅速

CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
// change properties here without animation
CATransaction.commit()

23
投票

除了Brad Larson's answer:对于自定义图层(由您创建)you can use delegation而不是修改图层的actions字典。这种方法更具动态性,可能更具性能。它允许禁用所有隐式动画,而无需列出所有可设置动画的键。

不幸的是,使用UIViews作为自定义层委托是不可能的,因为每个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中是没用的,因为实际的重绘可能(有时可能会)稍后发生。好的方法是使用自定义属性,如in this answer所述。


8
投票

这是一个更有效的解决方案,类似于接受的答案,但对于Swift。对于某些情况,每次修改一个性能问题的值时创建一个事务会比其他人提到的更好,例如常见的用例 - 以60fps左右拖动图层位置。

// Disable implicit position animation.
layer.actions = ["position": NSNull()]      

请参阅apple的how layer actions are resolved文档。实现委托会在级联中跳过一个级别,但在我的情况下由于caveat about the delegate needing to be set to the associated UIView而太乱了。

编辑:更新,感谢评论者指出NSNull符合CAAction


7
投票

根据Sam的回答和Simon的困难...在创建CSShapeLayer后添加委托引用:

CAShapeLayer *myLayer = [CAShapeLayer layer];
myLayer.delegate = self; // <- set delegate here, it's magic.

......“m”文件中的其他地方......

基本上与Sam没有通过自定义“disableImplicitAnimations”变量排列切换的能力相同。更多的是“硬线”方法。

- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event {

    // disable all implicit animations
    return (id)[NSNull null];

    // allow implicit animations
    // return nil;

    // you can also test specific key names; for example, to disable bounds animation:
    // if ([event isEqualToString:@"bounds"]) return (id)[NSNull null];

}

7
投票

实际上,我没有找到任何合适的答案。解决这个问题的方法是这样的:

- (id<CAAction>)actionForKey:(NSString *)event {   
    return nil;   
}

然后你可以使用它中的任何逻辑来禁用特定的动画,但是因为我想要将它们全部删除,所以我返回了nil。


5
投票

找到一个更简单的方法来禁用CATransaction内部的动作,该动作在内部调用setValue:forKey:作为kCATransactionDisableActions键:

[CATransaction setDisableActions:YES];

迅速:

CATransaction.setDisableActions(true)

3
投票

在Swift中禁用隐式图层动画

CATransaction.setDisableActions(true)
© www.soinside.com 2019 - 2024. All rights reserved.