抖动搜索栏取消/结束编辑动画

Jittery Search Bar Cancel / End Editing Animation

我有一个 SearchBar,我正在努力使其表现得像您在 iPhone 设置中看到的那样。当它被点击时,动画平滑地将放大镜从搜索栏的中间过渡到搜索栏的左侧,并平滑地引入取消按钮。但是,当我按下取消时,动画冻结然后跳回正常,搜索图标位于栏中间,取消按钮隐藏(参见 GIF 预览)。我的代码中有什么东西让这发生了吗?这是搜索条码:

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    self.view.endEditing(true)
    searchBar.setShowsCancelButton(false, animated: true)
}

输入你需要的代码

public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool

不确定为什么必须在 shouldEnd 中隐藏按钮而在 didBegin 中显示按钮

public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(true, animated: true)
}

public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.setShowsCancelButton(false, animated: true)
    return true
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}