Jittery Search Bar取消/结束编辑动画

时间:2017-02-27 17:53:12

标签: ios swift ios-animations

我有一个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)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

将您需要的代码放入

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()
}

enter image description here