I want to do a linear animation like this: move the rectangle slowly to center of Screen, and tripled its size while moving.
Here is my wrong code and demo, I can only do animation one by one, but I want to combine this two animation. Also, the end position is not right.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
presentAnimation()
}
private lazy var rectView: UIView = {
let view = UIView()
view.backgroundColor = .yellow
view.layer.borderColor = UIColor.blue.cgColor
return view
}()
}
extension ViewController {
private func setupUI() {
view.addSubview(rectView)
rectView.snp.makeConstraints {
$0.left.equalToSuperview().inset(40)
$0.top.equalToSuperview().inset(100)
$0.width.equalTo(40)
$0[![enter image description here][1]][1].height.equalTo(40)
}
}
private func presentAnimation() {
let testEnlarge = CABasicAnimation(keyPath: "transform.scale")
testEnlarge.fromValue = 1
testEnlarge.toValue = 3
testEnlarge.beginTime = CACurrentMediaTime() + 2
testEnlarge.duration = 1
testEnlarge.fillMode = .forwards
testEnlarge.isRemovedOnCompletion = false
let positionX = view.center.x - ((rectView.frame.width * 3)/2)
let positionY = view.center.y - ((rectView.frame.height * 3)/2)
let testTranslation = CAKeyframeAnimation(keyPath: "transform.translation")
testTranslation.values = [CGPoint(x: positionX, y: positionY)]
testTranslation.beginTime = CACurrentMediaTime() + 2
testEnlarge.duration = 1
testEnlarge.fillMode = .forwards
testTranslation.isRemovedOnCompletion = false
CATransaction.begin()
rectView.layer.add(testEnlarge, forKey: nil)
rectView.layer.add(testTranslation, forKey: nil)
CATransaction.commit()
}
}
Here is my **wrong demo**: