到目前为止,我已经能够为 CALayer 子类的自定义属性设置动画,这要归功于 + (BOOL)needsDisplayForKey:(NSString *)key
和 CABasicAnimations
。
然而事实证明,链接动画可能会变得非常棘手,因为所有代码都发生在单个 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];
抱歉,我现在没有可以轻松测试此功能的项目设置,但我相信它会起作用。
检查这些网站的代码:
- http://www.mentalfaculty.com/mentalfaculty/Blog/Entries/2010/9/22_FLIPPIN_OUT_AT_NSVIEW.html
- http://boondoggle.atomicwang.org/lemurflip/MFFlipController.m
我引用的代码:
[CATransaction begin];
[topLayer addAnimation:topAnimation forKey:@"flip"];
[bottomLayer addAnimation:bottomAnimation forKey:@"flip"];
[CATransaction commit];
关于iphone - 在 CATransaction 中对自定义 CALayer 属性进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4122348/