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

问题描述 投票:0回答:2

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

    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)
       }}
ios swift3 textview jquery-animate contentoffset
2个回答
0
投票

最简单的方法是使用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)


     })
})

0
投票

通过使用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
            })
© www.soinside.com 2019 - 2024. All rights reserved.