Search code examples
iosswiftanimationuiviewanimationcgaffinetransform

(Scale) Transform Animation on UITextView


My goal is to create a "bubble-up" animation to a UITextView that is being nested in a UIView card presented modally.

While the card appears, the UITextView doesn't seem to..is there some workaround for this? Below is the code I have tried thus far...

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    DispatchQueue.main.async {
        UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
            self.cardView.transform = CGAffineTransform(scaleX: 1, y: 1)              
            self.messageTextView.transform = CGAffineTransform(scaleX: 1, y: 1)  
        }, completion: nil)            
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    cardView.transform = CGAffineTransform(scaleX: 0, y: 0)
    messageTextView.transform = CGAffineTransform(scaleX: 0, y: 0)
}

Solution

  • I'm not sure the exact setting, so show the storyboard. I did not add the constrain but setNeedsLayout before animation. Hope it works for you.

    enter image description here

              override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            DispatchQueue.main.async {
                self.messageTextView.setNeedsLayout()
                UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
                    self.cardView.transform = CGAffineTransform(scaleX: 1, y: 1)
                    self.messageTextView.transform = CGAffineTransform(scaleX: 1, y: 1)
                }, completion: nil)
            }
        }
    

    enter image description here