iphone - 在 CATransaction 中对自定义 CALayer 属性进行动画处理

标签 iphone properties core-animation catransaction

到目前为止,我已经能够为 CALayer 子类的自定义属性设置动画,这要归功于 + (BOOL)needsDisplayForKey:(NSString *)keyCABasicAnimations

然而事实证明,链接动画可能会变得非常棘手,因为所有代码都发生在单个 animationDidStop:finished: 方法中。

所以我想切换到 CATransactions 因为它们支持新的 block 语法,这将允许我使用 + (void)setCompletionBlock:(void (^)( void))block.

但在我看来,CATransaction 只能为所谓的“可动画属性”设置动画,并且它不适用于我的自定义图层属性,即使使用 needsDisplayForKey: 方法已实现。

那么有没有办法在 CALayer 中创建自定义属性以使用 CATransaction 进行动画处理?

编辑: 我的意图是做一些事情:

[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

myLayer.myProperty = newValue;

[CATransaction commit];

myProperty 值到 newValue 的更新没有动画。我尝试过实现 actionForLayer:forKey: 在管理 myLayer 的 View 中返回一个 CABasicAnimation。但是 actionForLayer:forKey: 永远不会使用 myProperty 键调用。是的,myLayer 不是 view.layer,而是一个子层,是的,我将 myLayer 的委托(delegate)设置为包含 View 。

最佳答案

根据我对一些源代码的阅读,我相信您仍然可以在 CATransaction 中使用 CABasicAnimation。在 [CATransaction begin][CATransaction commit] 之间添加的任何 CAAnimations 都应该是事务的一部分。

[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

// Create the CABasicAnimation using your existing code
CABasicAnimation *myPropertyAnim = [CABasicAnimation animationWithKeyPath:@"myProperty"];
// TODO: Setup animation range
myPropertyAnim.toValue = newValue;

// The CATransaction does not observe arbitrary properties so this fails:
//myLayer.myProperty = newValue;

// Add the CAAnimation subclass during the CATransaction
[myLayer addAnimation:myPropertyAnim forKey:@"myKey"];

[CATransaction commit];

抱歉,我现在没有可以轻松测试此功能的项目设置,但我相信它会起作用。

检查这些网站的代码:

我引用的代码:

[CATransaction begin];
[topLayer addAnimation:topAnimation forKey:@"flip"];
[bottomLayer addAnimation:bottomAnimation forKey:@"flip"];
[CATransaction commit];

关于iphone - 在 CATransaction 中对自定义 CALayer 属性进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4122348/

相关文章:

ios - 如何打开像照片 native 应用程序一样的相册列表?

c# - 委托(delegate)给属性(property)

javascript - 为什么我的对象的属性没有记录到控制台?

swift - NSView 和 CALayer

ios - CATransform3D:计算透视变换后的高度

ios - CALayer mask - 合并两层

objective-c - 关于 iPhone SDK,您希望早点知道什么?

ios - iOS 9 及更低版本的 UIApplication.shared().open() 的替代方法是什么?

iphone - iOS 6 应用程序 - 如何处理 iPhone 5 屏幕尺寸?

java - eclipse中的属性文件保存在哪里?