Reputation: 789
I am trying to animate UITextView by changing transform. But it makes text disappered while animating. Here is my test code.
class ViewController: UIViewController {
let textView = UITextView()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 300).isActive = true
textView.backgroundColor = .systemOrange
textView.text = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
textView.textColor = .white
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
let animator = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.textView.transform = CGAffineTransform(translationX: 0, y: 1000)
})
animator.startAnimation()
}
}
}
I tried with storyboard but the result was same. Same result when I use UIView.animate
method.
Why does this happen? I want to animate it with text on.
let contraint = textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
contraint.isActive = true
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
contraint.constant = 1000
let animator = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.layoutIfNeeded()
})
animator.startAnimation()
}
I tested with this code, and got same result.
Upvotes: 5
Views: 622
Reputation: 536009
I'm afraid you'll have to animate a snapshot view instead:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
let v = self.textView.snapshotView(afterScreenUpdates: true)!
v.frame = self.textView.frame
self.view.addSubview(v)
self.textView.isHidden = true
let animator = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
v.frame.origin.y = 1000
})
animator.startAnimation()
}
I don't think that's a bad solution; this is what snapshot views are for, after all.
Upvotes: 4