如何跳过动画的一部分?
How to skip a part of an animation?
我有一个视图最初位于 A 位置,另外两个位置名为 B 和 C.
此图显示了视图的初始状态。
我想要实现的是将视图从 B 动画化为 C, 但方式不同.
我希望动画对象从 A 开始并在 C 结束,但跳过 A→B部分,只执行B→C部分。
代码应如下所示:
// aLabel and cLabel are the labels shown in the image above
let a = aLabel.center.x
let c = cLabel.center.x
let animation = CABasicAnimation(keyPath: "position.x")
animation.duration = 3
// The animation should start in code from A
animation.fromValue = a
// And ends at C
animation.toValue = c
// Update the model layer
someView.layer.position.x = c
/* ADD SOMETHING TO MAKE THE ANIMATION
STARTS FROM B AND ENDS AT C */
我已经尝试过的
我尝试将动画偏移设置为 1.5:
animation.timeOffset = 1.5
但是没有用。动画执行如下:
B → C → A → B ,我只要B → C
要跳转A→B部分,可以将fromValue
设置为bLabel居中
animation.fromValue = bLabel.center.x
如果您想保留 fromValue
和 toValue
,请设置 repeatDuration
以及 timeOffset
:
animation.timeOffset = 1.5
animation.repeatDuration = 1.5
我有一个视图最初位于 A 位置,另外两个位置名为 B 和 C.
此图显示了视图的初始状态。
我想要实现的是将视图从 B 动画化为 C, 但方式不同.
我希望动画对象从 A 开始并在 C 结束,但跳过 A→B部分,只执行B→C部分。
代码应如下所示:
// aLabel and cLabel are the labels shown in the image above
let a = aLabel.center.x
let c = cLabel.center.x
let animation = CABasicAnimation(keyPath: "position.x")
animation.duration = 3
// The animation should start in code from A
animation.fromValue = a
// And ends at C
animation.toValue = c
// Update the model layer
someView.layer.position.x = c
/* ADD SOMETHING TO MAKE THE ANIMATION
STARTS FROM B AND ENDS AT C */
我已经尝试过的
我尝试将动画偏移设置为 1.5:
animation.timeOffset = 1.5
但是没有用。动画执行如下: B → C → A → B ,我只要B → C
要跳转A→B部分,可以将fromValue
设置为bLabel居中
animation.fromValue = bLabel.center.x
如果您想保留 fromValue
和 toValue
,请设置 repeatDuration
以及 timeOffset
:
animation.timeOffset = 1.5
animation.repeatDuration = 1.5