简体   繁体   English

如何在Swift的搜索栏中制作动画?

[英]How to animate in a search bar in Swift?

I need some help with animiting (sliding in) a search bar using swift, I've tried looking up tutorials etc but I can't seem to find anything. 我需要一些帮助,使用swift来限制(滑入)搜索栏,我已经尝试查找教程等,但我似乎无法找到任何东西。 A visual representation would be something like this: 视觉表示将是这样的:

When I click the magnifying glass icon on the right of the navigation bar, I want the search bar to slide in from the left. 当我单击导航栏右侧的放大镜图标时,我希望搜索栏从左侧滑入。 All the tutorials that I found were about creating search bars under navigation bars or static ones etc. 我找到的所有教程都是关于在导航栏或静态栏目下创建搜索栏等。

You can check out navigationItem.titleView to place the search bar directly on the navigation bar, but implementing the animation you desire will be a very tricky. 您可以查看navigationItem.titleView将搜索栏直接放在导航栏上,但实现您想要的动画将非常棘手。 Also take a look at this question which contains some tips regarding your question. 另请查看问题,其中包含有关您问题的一些提示。

First i declare the search bar: 首先我声明搜索栏:

var searchBar: UISearchBar!

on viewDidLoad() function i initiated the searchbar as follows: 在viewDidLoad()函数上,我启动了搜索栏,如下所示:

self.searchBar = UISearchBar(frame: CGRect(x:300, y:0, width:300, height:20))

When the magnifying glass is clicked, i run the following code: 单击放大镜时,我运行以下代码:

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

Hope this helps! 希望这可以帮助!

You could implement UISearchBarDelegate and play with some kind of alpha or size animations like this: 您可以实现UISearchBarDelegate并使用某种类型的alpha或大小的动画,如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM