Xcode:如何在CATransaction中进行线性动画

时间:2016-07-26 22:15:30

标签: ios objective-c animation core-animation

我的交易开始缓慢,变得快,然后变慢。 我需要整个线速度。

我找到UIViewAnimationOptionCurveLinear但找不到[CATransaction begin]

的示例

这是我的代码:

[ CATransaction begin ]; 
    if( graph_animation_enable )
        [CATransaction setAnimationDuration: graph_animation_seconds ];
    else
        [CATransaction setAnimationDuration: 0 ];

    //[CATransaction setValue : ( id ) kCFBooleanTrue forKey : kCATransactionDisableActions]; 
    graph_CALayer.frame = CGRectMake( left_x, top_y, width, height );
    graph_CALayer.backgroundColor = bar_background_color.CGColor;

    CAMediaTimingFunction *linearTiming = 
      [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
    [CATransaction setAnimationTimingFunction: linearTiming];

[CATransaction commit];

我试过了答案,但仍然是非线性的。 动画开始,但随后变慢。

2 个答案:

答案 0 :(得分:0)

使用CATransaction setAnimationTimingFunctionkCAMediaTimingFunctionLinear

的时间价值

代码如下所示:

CAMediaTimingFunction *linearTiming = 
  [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
[CATransaction setAnimationTimingFunction: linearTiming]

答案 1 :(得分:0)

在Swift 4.2中,CATransaction有一个名为setAnimationTimingFunction(_:)的方法。 setAnimationTimingFunction(_:)具有以下声明:

class func setAnimationTimingFunction(_ function: CAMediaTimingFunction?)
  

设置用于该交易组内所有动画的计时功能。 [...]这是一种方便的方法,可以将CAMediaTimingFunction键的value(forKey:)值设置为kCATransactionAnimationTimingFunction


因此,您可以使用以下代码片段之一来为CATransaction设置线性动画:

CATransaction.begin()
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear))
/* ... */
CATransaction.commit()
CATransaction.begin()
CATransaction.setValue(CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear), forKey: kCATransactionAnimationTimingFunction)
/* ... */
CATransaction.commit()