Bronze Challenge: Spring Animations


#1

Here it is. So the extra two options usingSpringWithDamping and initialSpringVelocity do what they say: they give the animation a spring effect. To find more simply copy this code into xcode and option-click on either of the properties and their descriptions will pop up.

func animateLabelTransitions() {
    view.layoutIfNeeded()
    let screenWidth = view.frame.width
    self.nextQuestionLabelCenterXConstraint.constant = 0
    self.currentQuestionLabelCenterXConstraint.constant += screenWidth
    
    UIView.animate(withDuration: 0.5, animations: {
        self.currentQuestionLabel.alpha = 0
        self.nextQuestionLabel.alpha = 1
    })
    
    UIView.animate(withDuration: 0.5,
        delay: 0,
        usingSpringWithDamping: 0.5,
        initialSpringVelocity: 0.5,
        options: [],
        animations: {
            self.currentQuestionLabel.alpha = 0
            self.nextQuestionLabel.alpha = 1
            
            self.view.layoutIfNeeded()
        },
        completion: { _ in
            swap(&self.currentQuestionLabel, &self.nextQuestionLabel)
            swap(&self.currentQuestionLabelCenterXConstraint, &self.nextQuestionLabelCenterXConstraint)
            self.updateOffScreenLabel()
        }
    )
}