如何在textview上进行序列动画(swift3)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在textview上进行序列动画(swift3)相关的知识,希望对你有一定的参考价值。

我的下面的代码是一个按钮,当点击应用动画。现在有两个动画。我想做一个动画的序列意味着第二个动画在第一个动画完成之前不会开始。

    var speed: CGFloat = 5.3 // speed in seconds
@IBAction func press(_ sender: Any) {
    self.theTextView.resignFirstResponder()

    UIView.animate(withDuration: TimeInterval(speed), animations: {
            ////1st action[
               self.theTextView.contentOffset = .zero
               self.theTextView.setContentOffset(.zero, animated: true)]
      /////2nd action[
        self.theTextView.contentOffset = CGPoint(x: 0, y: self.theTextView.contentSize.height)]

    }, completion: nil)
       }}
答案

最简单的方法是使用animate(withDuration:animations:completion:)方法的完成块。当动画序列结束时执行完成块。在你的情况下,它看起来像这样:

UIView.animate(withDuration: 6.0, animations: {

     // First animation goes here

     self.theTextView.contentOffset = CGPoint.zero

}, completion: { (finished) in

     // This completion block is called when the first animation is done.

     // Make sure the animation was not interrupted/cancelled :

     guard finished else {
        return
     }

     UIView.animate(withDuration: 1.0, animations: {

         // And the second animation goes here

        self.theTextView.contentOffset = CGPoint(x: 0, y: self.theTextView.contentSize.height)


     })
})
另一答案

通过使用animateKeyframes,还可以方便地制作并发动画序列:

UIView.animateKeyframes(withDuration: 1, delay: 0, options: .calculationModeCubic, animations: {
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.3, animations: {
                    self.someView.alpha = 0.5
                    self.view.layoutIfNeeded()
                })
                //second animation
                UIView.addKeyframe(withRelativeStartTime: 0.4, relativeDuration: 0.3, animations: {
                    self.someView.alpha = 1
                    self.view.layoutIfNeeded()
                })
            }, completion: { (finish) in
                // Do something on animation complete
            })

以上是关于如何在textview上进行序列动画(swift3)的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift:在同一个View Controller上进行过渡动画

iOS Swift3 TextView 行在错误的位置

CAAnimation 在一个点上进行变换旋转

试图只允许在单个特定行上进行滑动行为

Xamarin Android - 如何在textview动画期间阻止文本移动

android 自定义控件继承TextView