#14 swift animate 動畫練習(一)

UIView, animate, delay, curveEaseOut, viewWillAppear, viewDidAppear

Photo by Jannis Brandt on Unsplash
突發狀況:執行後動畫卻沒有跑出來!原來是我在拉auto layout習慣會把跟左右邊距離全都設置好,
造成物件的置中對齊基準雖然改變,但左右距離沒有變
物件還是留在原地,
沒有改變位置,當然也不會有動畫產生。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut) {
self.centerAlignUsername.constant += self.view.bounds.width
self.leadingUsername.constant += self.view.bounds.width
self.trallingUsername.constant -= self.view.bounds.width
self.view.layoutIfNeeded()
}
UIView.animate(withDuration: 0.5, delay: 0.3, options: .curveEaseOut) {
self.centerAlignPassword.constant += self.view.bounds.width
self.view.layoutIfNeeded()
}
UIView.animate(withDuration: 0.5, delay: 0.4, options: .curveEaseOut) {
self.myButton.alpha = 1
self.view.layoutIfNeeded()
}
}
@IBAction func myButton(_ sender: Any) {
let bounds = self.myButton.bounds
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 10) { //springWithDamping 彈力 1沒彈力 0最彈
self.myButton.bounds = CGRect(x: bounds.origin.x - 20, y: bounds.origin.y, width: bounds.size.width + 60, height: bounds.size.height)
self.myButton.isEnabled = false
}
}

Wish me luck on the way become iOS app developer!

Wish me luck on the way become iOS app developer!