1

I am adding and removing attributed text from a UITextView. I wish to use UIView.animate to add an animation to when text is appended to the text view and when that appended text is removed from the text view. So far, I have this, but it does not cause any noticeable animation on the text view:

UIView.animate(withDuration: 1, delay: 0, options: .CurveLinear, animations: {
    self.view.layoutIfNeeded()
    self.textView.attributedText = newAttributedText
}, completion: { finished in
    print("Animation completed")
}

// prints "Animation completed", but no animation occurs
2
  • Can you post where are you using this code? Jul 11, 2017 at 23:35
  • The attributedText (and text) attribute is not animatable using simple UIView animations.
    – rmaddy
    Jul 11, 2017 at 23:37

3 Answers 3

1

You cannot animate changing of text in that manner. There is a list of animatable properties of CALayer class and UIView class.

1

Set text cannot be animated by UIView.animate. Only changes like transparency, colors, shape, location, etc can be animated by the UIView.animate.

Here is an example of animating origin and transparency change with code looks like

self.textView.text = "test Animation"
self.textView.alpha = 0

UIView.animate(withDuration: 3, delay: 0, options: .curveLinear, animations: {
    var frame = self.textView.frame
    frame.origin.x = frame.origin.x + 50
    frame.origin.y = frame.origin.y + 50
    self.textView.frame = frame

    self.textView.alpha = 1

}, completion: { finished in
    print("Animation completed")
})

And animation looks like

enter image description here

1

Here is my solution (swift 3 ) :

let animator = UIViewPropertyAnimator(duration: 0.2, curve: .easeOut, animations: {
            self.textView2.frame = self.textView2.frame.offsetBy(dx: 20 , dy: 25)
        } )
        animator.startAnimation()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.