如何在Core Animation中为onOrderOut使用自定义动画? [英] How can I use custom animations for onOrderOut in Core Animation?

查看:314
本文介绍了如何在Core Animation中为onOrderOut使用自定义动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

核心动画允许通过在您的基于CALayer的类中实现actionForKey方法来创建自定义动画:

Core Animation allows for custom animations by implementing the actionForKey method in your CALayer based class:

- (id<CAAction>)actionForKey:(NSString *)key {
    // Custom animations
    return [super actionForKey:key];
}

然后我可以创建动画并返回 onOrderIn action(即当图层被添加到另一个图层时)。这工作得很好。如果我对 onOrderOut 做同样的操作(即图层从其上层中移除),则返回的动画将被忽略,并应用默认动画。

I can then create an animation and return it for the onOrderIn action (i.e. when the layer is added to another layer). This works just fine. If I do the same for onOrderOut (i.e. the layer is removed from its superlayer), the returned animation is ignored, and the default animation is applied instead.

我的目标是放大图层( onOrderIn )和out( onOrderOut ):

My goal is to zoom the layer in (onOrderIn) and out (onOrderOut):

- (id<CAAction>)actionForKey:(NSString *)key {

    if ([key isEqualToString:@"onOrderIn"] || [key isEqualToString:@"onOrderOut"]) {
        CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        a.duration = 0.25;
        a.removedOnCompletion = NO;
        a.fillMode = kCAFillModeBoth;

        if ([key isEqualToString:@"onOrderIn"]) {
            a.fromValue = [NSNumber numberWithFloat:0.0];
            a.toValue = [NSNumber numberWithFloat:1.0];
        } else {
            a.fromValue = [NSNumber numberWithFloat:1.0];
            a.toValue = [NSNumber numberWithFloat:0.0];
        }

        return a;
    }

    return [super actionForKey:key];
}

放大作品,缩小不会。而是使用默认淡出动画。

Zooming in works, zooming out does not. Instead the default fade out animation is used.

此代码可能包含一些打字错误,因为我在其他计算机上打字。

The code might contain some typos, as I'm typing this on another machine.

推荐答案

quartz-dev邮件列表



返回
onOrderOut键的任何动画的基本问题 - 在
动画应该运行时,层
不再在树中,因此它没有
效果。所以onOrderOut是没有用的
触发动画;它可能是
有用的运行其他代码时
层从树中删除。

There's a fundamental problem with returning any animation for the onOrderOut key—by the time the animation should be running, the layer is no longer in the tree, so it has no effect. So onOrderOut is not useful for triggering animations; it may be useful for running other code when layers are removed from the tree.

我找到的最好的解决方案
(假设父级上的默认淡入转换
不是你想要的,
,它通常不是)是添加自定义
动画来应用移除效果
你想要的话,然后在didStop
动画委托中,实际上删除
的图层。它通常方便的
创建一组动画
与委托属性设置和
fillMode =转,
removedOnCompletion = NO,以便您可以
删除层在
动画的末尾,而
层在其
正常状态下仍然可见。

The best solution I've found for this (assuming the default fade transition on the parent is not what you want, which it often isn't) is to add custom animations to apply the removal effect you want, then, in the didStop animation delegate, actually remove the layer. It's often convenient to create a single group of animations with the delegate property set, and fillMode=forwards, removedOnCompletion=NO so that you can remove the layer at the end of the animation with no possibility of the layer still being visible in its normal state.

如果你做了很多事情,很容易写一个公共的超类,启动一个动画,设置动画委托给类,并实现+ animationDidStop:以移除启用了w / o动画的图层。这恢复了CoreAnimation的火和忘记的性质,你希望它将与默认实现一起出现。

If you do many case of this, it is easy to write a common superclass that starts an animation, sets the animation delegate to the class and implements +animationDidStop: to remove the layer w/o animation enabled. This restores the fire-and-forget nature of CoreAnimation that you'd have hoped would be present with the default implementation.

这篇关于如何在Core Animation中为onOrderOut使用自定义动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆