I read and watched many tutorial about adding animation to labels but none of them work for me as I wanted. I want when I open a view controller, after a second, a UILabel start to fade in from the bottom of the page and come to the center of the page. for example here is the outlet of the label that I said about
@IBOutlet weak var welcomeLabel: UILablel!
I made this function and add it to the viewdidload
but it's not showing any animation, also when I tap on it will crash with this error:
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
here is the function:
func addAnimation() {
UILabel.animate(withDuration: 3, delay: 3, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.welcomeLabel.transform = CGAffineTransform(translationX: 0, y: -200)
})
}
I also know this is not the animation that I wated, I just add it for the test. Could you help me to write the right animation that I wanted:
Start to fade in and move from the bottom to the center of the page.
thank you so much
Create @IBOutlet
of UILabel
and put the label in the center of the screen and add the required constraint
.
Create @IBOutlet of label center Y alignment of Constraint
like below.
@IBOutlet weak var centerY: NSLayoutConstraint!
Then change its constant value.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
centerY.constant = self.view.bounds.height/2
self.view.layoutIfNeeded()
addAnimation()
}
func addAnimation(){
UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseOut], animations: {
self.centerY.constant = 0
self.welcomeLabel.alpha = 1.0
self.view.layoutIfNeeded()
}, completion: nil)
}