gpt4 book ai didi

ios - 关闭键盘通知触发动画的问题

转载 作者:行者123 更新时间:2023-11-30 12:30:04 25 4
gpt4 key购买 nike

我有一个占位符,当键盘打开和关闭时,我会为其设置动画。到目前为止,当键盘打开时,它可以正常显示动画,但是当键盘关闭时,它会保持在原位。下面是我正在使用的代码。

func setupKeyboardObservers() {

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

设置键盘通知

        let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow
let isKeybobardNotShowing = notification.name == NSNotification.Name.UIKeyboardWillHide

这里是动画发生的地方

        DispatchQueue.main.async {
if isKeyboardShowing {

NSLayoutConstraint(item: self.placeholder, attribute: .left, relatedBy: .equal, toItem: self.searchBarView, attribute: .left, multiplier: 1, constant: 5).isActive = true
UIView.animate(withDuration: 0.5, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed) in

})

}
}


DispatchQueue.main.async {
if isKeybobardNotShowing {

NSLayoutConstraint(item: self.placeholder, attribute: .centerX , relatedBy: .equal, toItem: self.searchBarView, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
UIView.animate(withDuration: 0.5, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed) in

})

}
}

有什么建议吗?

最佳答案

试试这个。在两个调度队列顶部再添加一次 self.view.layoutIfNeeded() ...

self.view.layoutIfNeeded()
DispatchQueue.main.async {
....
self.view.layoutIfNeeded()
}
}

关于ios - 关闭键盘通知触发动画的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com