我希望在子视图中有一个缩放动画(在这种情况下它是一个UILabel),但设置初始旋转而不动画它。问题是它从初始旋转动画回到0度。
recordedTime?.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
recordedTime?.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
view.addSubview(recordedTime!)
UIView.animate(withDuration: 0.50, delay: 0.0, options: [], animations: {
recordedTime?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}, completion:nil)
我想我可以在动画中重复旋转变换,但这似乎是多余的,而且我的实际代码更复杂,因为它检查我的屏幕方向,所以我不想再重复这些代码。
答案 0 :(得分:1)
我想我可以在动画中重复旋转变换,但这似乎是多余的
但它并不多余。由于视图的旋转和缩放编码在相同的transform
属性中,因此您别无选择:必须始终将transform
属性设置为包含所需的旋转和缩放。
您可以创建属性以分别存储旋转和缩放,然后根据这些属性计算transform
:
class ViewController: UIViewController {
@IBOutlet var recordedTime: UILabel!
private var recordedTimeRotation: CGFloat = 0 {
didSet { updateRecordedTimeTransform() }
}
private var recordedTimeScale: CGFloat = 1 {
didSet { updateRecordedTimeTransform() }
}
private func updateRecordedTimeTransform() {
var transform: CGAffineTransform = .identity
transform = transform.rotated(by: recordedTimeRotation)
transform = transform.scaledBy(x: recordedTimeScale, y: recordedTimeScale)
recordedTime?.transform = transform
}
private func run() {
recordedTimeScale = 0.1
recordedTimeRotation = .pi / 2
view.addSubview(recordedTime!)
UIView.animate(withDuration: 0.50, delay: 0.0, options: [], animations: {
recordedTimeScale = 1
}, completion:nil)
}
}