在CATransaction中动画自定义CALayer属性
到目前为止,我已经能够动画CALayer子类的自定义属性,这要归功于+ (BOOL)needsDisplayForKey:(NSString *)key和CABasicAnimations 。
然而,事实证明,链接动画可能变得非常棘手,因为所有的代码都发生在一个animationDidStop:finished:方法中。
所以我想切换到CATransactions因为它们支持新的块语法,这将允许我使用+ (void)setCompletionBlock:(void (^)(void))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不是动画。 我试图在管理myLayer的视图中实现actionForLayer:forKey:来返回一个CABasicAnimation 。 但actionForLayer:forKey:永远不会使用密钥myProperty调用。 是的, myLayer不是view.layer而是一个子图层,是的,我将myLayer的委托设置为包含视图。
我相信,根据我对某些源代码的阅读,您仍然可以在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];
有一个名为CAAnimationBlocks的优秀类,并在这里解释,这是CAAnimation的一个类别,它允许您像使用UIView一样使用完成块。
你只需调用以下方法来使用它:
CABasicAnimation myAnimation;
[myAnimation setCompletion:^(BOOL finished) { // Do something }];
链接地址: http://www.djcxy.com/p/74177.html
上一篇: Animate custom CALayer properties inside a CATransaction
下一篇: Custom animation of two properties in CALayer by a new custom property
