We have an animation that translates, rotates and scales. After 2 seconds we want it to translate, rotate and scale backwards to its original position. Instead of reversing back to its original position, it just pops back to the starting position and whatever scale or rotate values that were given to it to transform to its original size and shape, it's now placed on the original object instead, as if the first transform never happened.
class ViewController: UIViewController {
var customView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
customView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
customView.backgroundColor = UIColor.blue
customView.layer.cornerRadius = 25
customView.layer.borderWidth = 8
customView.layer.borderColor = UIColor.red.cgColor
self.view.addSubview(customView)
}
override func viewDidAppear(_ animated: Bool) {
let translate = CGAffineTransform(translationX: 50, y: 70)
UIView.animate(withDuration: 2, delay: 1, options: .curveEaseInOut, animations: {
self.customView.transform = translate.rotated(by: CGFloat.pi/2).scaledBy(x: 1, y: 0.5)
}, completion: {_ in
let translate2 = CGAffineTransform(translationX: 10, y: 10)
UIView.animate(withDuration: 2, delay: 0, options: .curveEaseInOut, animations: {
self.customView.transform = translate2.rotated(by: -CGFloat.pi/2).scaledBy(x: 1, y: -0.5)
}, completion: {_ in
})
})
}
}