gpt4 book ai didi

objective-c - 如何在 Core Animation 中为 onOrderOut 使用自定义动画?

转载 作者:太空狗 更新时间:2023-10-30 03:33:34 26 4
gpt4 key购买 nike

Core Animation 允许通过在基于 CALayer 的类中实现 actionForKey 方法来实现自定义动画:

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

然后我可以创建一个动画并将其返回给 onOrderIn 操作(即,当图层添加到另一个图层时)。这很好用。如果我对 onOrderOut 执行相同的操作(即从其超层中删除该层),则返回的动画将被忽略,而是应用默认动画。

我的目标是放大图层 (onOrderIn) 和缩小图层 (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];
}

放大有效,缩小无效。取而代之的是使用默认的淡出动画。

代码可能包含一些拼写错误,因为我是在另一台机器上输入的。

有人能帮忙吗?

最佳答案

quartz-dev mailing list 上引用 John Harper :

There's a fundamental problem withreturning any animation for theonOrderOut key—by the time theanimation should be running, the layeris no longer in the tree, so it has noeffect. So onOrderOut is not usefulfor triggering animations; it may beuseful for running other code whenlayers are removed from the tree.

The best solution I've found for this(assuming the default fade transitionon the parent is not what you want,which it often isn't) is to add customanimations to apply the removal effectyou want, then, in the didStopanimation delegate, actually removethe layer. It's often convenient tocreate a single group of animationswith the delegate property set, andfillMode=forwards,removedOnCompletion=NO so that you canremove the layer at the end of theanimation with no possibility of thelayer still being visible in itsnormal state.

如果你做了很多这样的事情,很容易写一个通用的父类(super class)来启动动画,将动画委托(delegate)设置给类并实现 +animationDidStop: 来移除层 w/o启用动画。这恢复了 CoreAnimation 的即发即弃性质,您希望它会出现在默认实现中。

关于objective-c - 如何在 Core Animation 中为 onOrderOut 使用自定义动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/243266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com