完成块中的UIView动画以位移开始

时间:2017-10-20 09:37:35

标签: ios swift uiviewanimation

我使用UIView.animate编写了一个带有两个动画的简单动画链,但是完成块中的第二个动画并不是从第一个动画的确切位置开始,所以我向右移动了奇怪的位移。有人可以帮忙吗?也许我没有完全理解tranfrorm属性。

UIView.animate(withDuration: 3, animations: {
    self.redView.transform = self.redView.transform.translatedBy(x: 100, y: 0)
}) { (_) in
    UIView.animate(withDuration: 2, animations: {
        self.redView.transform = self.redView.transform.scaledBy(x: 2, y: 2)
    })
}

我的redView应该在100上向右移动,然后从同一个地方变成两倍大。但在第二次动画之前,右侧有位移。我不知道为什么会这样。 谢谢!

此问题的Gif:

enter image description here

1 个答案:

答案 0 :(得分:2)

不确定你的意图是什么,但我会在第一个区块中为框架设置动画:

let initialFrame = redView.frame
UIView.animate(withDuration: 3, animations: {
    self.redView.frame = initialFrame.offsetBy(dx: 100, dy: 0)
}) { (_) in
    UIView.animate(withDuration: 2, animations: {
        self.redView.transform = self.redView.transform.scaledBy(x: 2, y: 2)
    })
}