2017-09-18 13 views
1

Mon code ci-dessous est un bouton qui, lorsqu'il est frappé, applique une animation. En ce moment il y a deux animations. Je voudrais faire une séquence qui signifie que la deuxième animation ne démarre pas avant la fin de la première animation.Comment faire une animation de séquence sur une vue de texte (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) 
     }} 

Répondre

0

La meilleure façon de le faire serait d'utiliser le bloc d'achèvement de la méthode animate(withDuration:animations:completion:). Le bloc d'achèvement est exécuté lorsque la séquence d'animation se termine. Dans votre cas, il ressemblerait à ceci:

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

Il y a aussi moyen pratique de faire la séquence d'animations simultanées en utilisant 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 
      })