Ios 如何在Swift中的搜索栏中设置动画?

Ios 如何在Swift中的搜索栏中设置动画?,ios,swift,uisearchbar,uiviewanimation,Ios,Swift,Uisearchbar,Uiviewanimation,我需要一些使用swift在搜索栏中进行动画制作(滑动)的帮助,我尝试过查找教程等,但似乎找不到任何东西。 视觉表现应该是这样的: class MyViewController: UIViewController, UISearchBarDelegate { func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { UIView.animate(withDuration: 0.6, animations:

我需要一些使用swift在搜索栏中进行动画制作(滑动)的帮助,我尝试过查找教程等,但似乎找不到任何东西。 视觉表现应该是这样的:

class MyViewController: UIViewController, UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

        UIView.animate(withDuration: 0.6, animations: { /*animate here*/ }, completion:  nil)
    }
}


当我单击导航栏右侧的放大镜图标时,我希望搜索栏从左侧滑入。我找到的所有教程都是关于在导航栏或静态栏下创建搜索栏等的。

您可以查看
navigationItem。titleView
将搜索栏直接放置在导航栏上,但实现所需的动画将是一个非常棘手的问题。另请看一看问题,其中包含有关您的问题的一些提示。

首先,我声明搜索栏:

var searchBar: UISearchBar!
在viewDidLoad()函数上,我启动了搜索栏,如下所示:

self.searchBar = UISearchBar(frame: CGRect(x:300, y:0, width:300, height:20))
单击放大镜时,我运行以下代码:

UIView.animate(withDuration: 0.25) {
    self.searchBar.frame = CGRect(x:0, y:0, width:300, height:20)
}

希望这有帮助

您可以实现UISearchBarDelegate,并使用类似以下的alpha或大小动画:

class MyViewController: UIViewController, UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

        UIView.animate(withDuration: 0.6, animations: { /*animate here*/ }, completion:  nil)
    }
}

谢谢,我来看看!