UIView.animateWithDuration 不应用转换
UIView.animateWithDuration doesn't apply transform
我正在尝试在两个视图之间创建旋转过渡效果。为此,我使用比例变换。
我的问题是第一个动画由于某种原因没有动画:视图就消失了。第二个动画按预期执行。有任何想法吗?谢谢
private func replace(
_ source: UIView,
with destination: UIView,
usingRotationEffect: Void
) {
let collapsed = CGAffineTransform(scaleX: 0, y: 1)
let expanded = CGAffineTransform.identity
source.isHidden = false
source.transform = expanded
destination.isHidden = true
destination.transform = collapsed
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
source.transform = collapsed
},
completion: { _ in
source.isHidden = true
destination.isHidden = false
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
destination.transform = expanded
})
})
}
这是一个已知问题,即 CGAffineTransform 中的零比例未正确设置动画。
这是因为它使用矩阵计算每一帧的变换,并且比例因子为零,因此无法执行这些计算
因此,解决方法是使用 非常小但非零的 值,例如:
private func replace(
_ source: UIView,
with destination: UIView,
usingRotationEffect: Void
) {
let collapsed = CGAffineTransform(scaleX: 0.0001, y: 1)
let expanded = CGAffineTransform.identity
source.isHidden = false
source.transform = expanded
destination.isHidden = true
destination.transform = collapsed
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
source.transform = collapsed
},
completion: { _ in
source.isHidden = true
destination.isHidden = false
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
destination.transform = expanded
})
})
}
我正在尝试在两个视图之间创建旋转过渡效果。为此,我使用比例变换。
我的问题是第一个动画由于某种原因没有动画:视图就消失了。第二个动画按预期执行。有任何想法吗?谢谢
private func replace(
_ source: UIView,
with destination: UIView,
usingRotationEffect: Void
) {
let collapsed = CGAffineTransform(scaleX: 0, y: 1)
let expanded = CGAffineTransform.identity
source.isHidden = false
source.transform = expanded
destination.isHidden = true
destination.transform = collapsed
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
source.transform = collapsed
},
completion: { _ in
source.isHidden = true
destination.isHidden = false
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
destination.transform = expanded
})
})
}
这是一个已知问题,即 CGAffineTransform 中的零比例未正确设置动画。 这是因为它使用矩阵计算每一帧的变换,并且比例因子为零,因此无法执行这些计算
因此,解决方法是使用 非常小但非零的 值,例如:
private func replace(
_ source: UIView,
with destination: UIView,
usingRotationEffect: Void
) {
let collapsed = CGAffineTransform(scaleX: 0.0001, y: 1)
let expanded = CGAffineTransform.identity
source.isHidden = false
source.transform = expanded
destination.isHidden = true
destination.transform = collapsed
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
source.transform = collapsed
},
completion: { _ in
source.isHidden = true
destination.isHidden = false
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveLinear,
animations: {
destination.transform = expanded
})
})
}