3

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
      })

    })
  }
}
jscs
  • 63,694
  • 13
  • 151
  • 195
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
  • 1
    @keithbhunter that question is from 2011, I'm looking for a swift 3 answer – SwiftyJD Jun 05 '17 at 20:33
  • 1
    It's the exact same code written in a slightly different syntax. Hopefully you can figure out what's going on since all the words in the code are the same. – keithbhunter Jun 05 '17 at 20:34
  • 1
    I'm having a hard time with the referenced post, all the code is deprecated. Would really like this question reopened – Marcus Kim Mar 10 '20 at 03:56

0 Answers0