如何跟踪动画状态
How to keep track of animation state
上下文
我在堆栈视图中有两个文本字段。当点击其中一个时,我需要它们同时向上动画。
当您从一个文本字段切换到下一个文本字段时,我希望它们都保持原样。它应该只在键盘被关闭时才回退动画。
由于两个文本字段都有自己的代理,从一个到下一个的点击会触发向下的动画,导致轻微的跳跃。跳转是我要删除的错误。
我尝试过的:
我创建了一个 Bool,如果动画已经发生,它应该保持跟踪。然而,通过自身附加改变其状态,以及通过方法触发它似乎不起作用。
func didAnimate() {
self.isAnimated = true
print("Didanimate result:" + String(isAnimated))
}
func animateUp() {
// moves views up to avoid keyboard
UIView.animate(withDuration: 0.5,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant += 200
//print("~~~~~~~~~")
self.view.layoutIfNeeded()
self.didAnimate()
print(self.isAnimated)
}, completion: { (finished: Bool) in
self.isAnimated = true
})
}
func animateDown() {
// moves views down after keyboard leaves
UIView.animate(withDuration: 1,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant -= 200
self.view.layoutIfNeeded()
self.isAnimated = false
}, completion: nil)
}
//MARK:- Text Field delegate
func textFieldDidBeginEditing(_ textField: UITextField) {
if (keyBoardIsVisible) {
// no need to animate anything
} else {
self.animateUp()
self.keyBoardIsVisible = true
}
print("initial animate result:" + String((isAnimated)))
}
func textFieldDidEndEditing(_ textField: UITextField) {
if(keyBoardIsVisible) {
animateDown()
self.keyBoardIsVisible = false
}
}
不要使用 textFieldDidBeginEditing
和 textFieldDidEndEditing
来处理键盘动画。因为无论何时切换到第二个文本字段 textFieldDidEndEditing
都会被调用。
我认为更好的方法是使用本地通知来处理这种情况,使用 Swift 3:
中的这两个通知
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(YourController.keyboardWillShow(sender:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(YourController.keyboardWillHide(sender:)),name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func animateUp() {
// moves views up to avoid keyboard
UIView.animate(withDuration: 0.5,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant += 200
//print("~~~~~~~~~")
self.view.layoutIfNeeded()
print(self.isAnimated)
}, completion: { (finished: Bool) in
})
}
func animateDown() {
// moves views down after keyboard leaves
UIView.animate(withDuration: 1,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant -= 200
self.view.layoutIfNeeded()
}, completion: nil)
}
func keyboardWillShow (notification: NSNotification) {
if (keyBoardIsVisible) {
return
} else {
self.animateUp()
self.keyBoardIsVisible = true
}
print("initial animate result:" + String((isAnimated)))
}
func keyboardWillHide (notification: NSNotification) {
if(keyBoardIsVisible) {
animateDown()
self.keyBoardIsVisible = false
}
}
上下文 我在堆栈视图中有两个文本字段。当点击其中一个时,我需要它们同时向上动画。
当您从一个文本字段切换到下一个文本字段时,我希望它们都保持原样。它应该只在键盘被关闭时才回退动画。
由于两个文本字段都有自己的代理,从一个到下一个的点击会触发向下的动画,导致轻微的跳跃。跳转是我要删除的错误。
我尝试过的: 我创建了一个 Bool,如果动画已经发生,它应该保持跟踪。然而,通过自身附加改变其状态,以及通过方法触发它似乎不起作用。
func didAnimate() {
self.isAnimated = true
print("Didanimate result:" + String(isAnimated))
}
func animateUp() {
// moves views up to avoid keyboard
UIView.animate(withDuration: 0.5,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant += 200
//print("~~~~~~~~~")
self.view.layoutIfNeeded()
self.didAnimate()
print(self.isAnimated)
}, completion: { (finished: Bool) in
self.isAnimated = true
})
}
func animateDown() {
// moves views down after keyboard leaves
UIView.animate(withDuration: 1,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant -= 200
self.view.layoutIfNeeded()
self.isAnimated = false
}, completion: nil)
}
//MARK:- Text Field delegate
func textFieldDidBeginEditing(_ textField: UITextField) {
if (keyBoardIsVisible) {
// no need to animate anything
} else {
self.animateUp()
self.keyBoardIsVisible = true
}
print("initial animate result:" + String((isAnimated)))
}
func textFieldDidEndEditing(_ textField: UITextField) {
if(keyBoardIsVisible) {
animateDown()
self.keyBoardIsVisible = false
}
}
不要使用 textFieldDidBeginEditing
和 textFieldDidEndEditing
来处理键盘动画。因为无论何时切换到第二个文本字段 textFieldDidEndEditing
都会被调用。
我认为更好的方法是使用本地通知来处理这种情况,使用 Swift 3:
中的这两个通知override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(YourController.keyboardWillShow(sender:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(YourController.keyboardWillHide(sender:)),name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func animateUp() {
// moves views up to avoid keyboard
UIView.animate(withDuration: 0.5,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant += 200
//print("~~~~~~~~~")
self.view.layoutIfNeeded()
print(self.isAnimated)
}, completion: { (finished: Bool) in
})
}
func animateDown() {
// moves views down after keyboard leaves
UIView.animate(withDuration: 1,
delay: 0,
options: .curveEaseOut,
animations: {
self.textFieldsY.constant -= 200
self.view.layoutIfNeeded()
}, completion: nil)
}
func keyboardWillShow (notification: NSNotification) {
if (keyBoardIsVisible) {
return
} else {
self.animateUp()
self.keyBoardIsVisible = true
}
print("initial animate result:" + String((isAnimated)))
}
func keyboardWillHide (notification: NSNotification) {
if(keyBoardIsVisible) {
animateDown()
self.keyBoardIsVisible = false
}
}