在逐步应用转换而不是立即应用它们时,我看到了一些意想不到的、不一致的行为,我想知道为什么。
假设我们有一个标签,我们想向右100
和向下平移50
,然后放大到1.5
原始大小的倍数。所以有两种转换:
- 翻译
- 规模
假设我们正在试验两种不同的动画:
- 并行执行平移和缩放
- 执行平移,然后依次执行缩放
在第一个动画中,您可能会执行以下操作:
UIView.animate(withDuration: 5, animations: {
label.transform = label.transform.translatedBy(x: 100, y: 50).scaledBy(x: 1.5, y: 1.5)
}, completion: nil)
一切都按照您的预期进行。标签同时平滑地平移和缩放。
在第二个动画中:
UIView.animate(withDuration: 5, animations: {
label.transform = label.transform.translatedBy(x: 100, y: 50)
}, completion: { _ in
UIView.animate(withDuration: 5, animations: {
label.transform = label.transform.scaledBy(x: 1.5, y: 1.5)
}, completion: nil)
})
标签翻译正确,然后繁荣,它意外跳跃,然后开始缩放。
是什么导致了这种突然的、意想不到的跳跃?通过检查每个变换(并行和顺序变换)的矩阵,值是相同的,正如预期的那样。
并行动画
transform before translate and scale: CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
translate and scale transform: CGAffineTransform(a: 1.5, b: 0.0, c: 0.0, d: 1.5, tx: 100.0, ty: 50.0)
transform after translate and scale: CGAffineTransform(a: 1.5, b: 0.0, c: 0.0, d: 1.5, tx: 100.0, ty: 50.0)
顺序动画
transform before translation: CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
translation transform: CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 100.0, ty: 50.0)
transform after translation: CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 100.0, ty: 50.0)
transform before scale: CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 100.0, ty: 50.0)
scale transform: CGAffineTransform(a: 1.5, b: 0.0, c: 0.0, d: 1.5, tx: 100.0, ty: 50.0)
transform after scale: CGAffineTransform(a: 1.5, b: 0.0, c: 0.0, d: 1.5, tx: 100.0, ty: 50.0)
那么是什么导致了突然的跳跃呢?