解除文本字段的键盘时,UIView动画不起作用



使用UITextFieldDelegate,我有两个函数来控制文本字段开始编辑和结束的情况。

使用UIView.animate时,我正在使用.isHidden.alpha属性使一个按钮消失并隐藏。

之所以使用.alpha,是因为我还设置了按钮消失的动画,并有一点延迟。

虽然消失效果很好,但在使用textFieldDidEndEditing时,动画不起作用,按钮确实会回来,但它会突然不容易地回来。

为了关闭键盘,我允许用户在ViewDidLoad()中使用键盘敲击任何位置

self.view.addGestureRecognizer(UITapGestureRecognizer(target:     
self.view, action: #selector(UIView.endEditing(_:))))

我使用的代理函数如下:

extension ExampleViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == exampleTextField {
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveEaseOut,
animations: {self.exampleButton.alpha = 0.0},
completion: { _ in self.exampleButton.isHidden = true
})
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == exampleTextField {
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveEaseInOut,
animations: {self.exampleButton.alpha = 1.0},
completion: { _ in self.exampleButton.isHidden = false
//Do anything else that depends on this animation ending
})
}
}

}

为什么.curveEaseIn不工作?

问题是按钮被隐藏了。当它仍然隐藏时,您将其淡入,然后在完成处理程序中取消隐藏alpha为1的按钮。这使得它在0.2秒后突然出现。

由于使用alpha为0进行隐藏,而使用alpha为1进行显示,因此在任何一个动画中都不需要设置isHidden属性。

最新更新