【问题标题】:UIView animation not working when dismissing keyboard for textfield关闭文本字段的键盘时 UIView 动画不起作用
【发布时间】:2019-11-07 06:33:09
【问题描述】:

使用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 不起作用?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

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

    由于您使用 0 的 alpha 隐藏并使用 1 的 alpha 显示,因此根本不需要在任一动画中设置 isHidden 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      相关资源
      最近更新 更多