
如何覆盖退出属于UISearchController的searchbar的默认动画?
标准搜索控制器行为:
好的,所以我试图创建一个自定义动画,当连接到UISearchController的UISearchbar变为活动时.标准动画似乎希望searchbar以占用屏幕的宽度开始.当动画开始时,缩小搜索范围并在其右侧的取消按钮中渐变.
我想要实现的
我想让我的searchbar开始在一个小的状态,大约是屏幕宽度的一半,以允许两个按钮放置在旁边的导航栏中.
现在动画:
当searchbar变为活动时,我想让动画展开searchbar,取消按钮淡入.
取消动画:
当searchbar被关闭时,我想要发生恰好相反的动画:取消按钮淡出,并且searchbar收缩到原始大小.
问题:
我已经找到了一种通过使用UISearchControllerDelegate方法来实现所需呈现动画的方法,presentSearchController:
func presentSearchController(searchController: UISearchController) { // Animate buttons UIVIEw.animateWithDuration(0.1,animations: { // First HIDe buttons self.createMoxybutton.Alpha = 0 self.centerMapbutton.Alpha = 0 }) // Animate Search bar UIVIEw.animateWithDuration(0.5,animations: { // Search bar searchController.searchbar.frame = CGRectMake(searchController.searchbar.frame.origin.x,searchController.searchbar.frame.origin.y,self.wBounds - 40,searchController.searchbar.frame.height) self })} 但我没有能够实现解雇动画.我已经尝试使用dIDdismissSearchController:和willdismissSearchController:委托方法,但它会导致奇怪的行为,并且不使用我在这些相应的委托方法中设置的帧的动画.当Searchbar被关闭时,它将扩展到全屏宽度,而淡出取消按钮,则会立即将searchbar的框架更改回原来的大小,忽略我的动画.我也尝试使用removeAllAnimation()方法尝试停止默认的动画发生,但是没有用.
func dIDdismissSearchController(searchController: UISearchController) { searchController.searchbar.layer.removeAllAnimations() // Animate UIVIEw.animateWithDuration(0.5,animations: { // Show hIDden buttons self.createMoxybutton.Alpha = 1 self.centerMapbutton.Alpha = 1 // Search bar searchController.searchbar.frame = CGRectMake(searchController.searchbar.frame.origin.x,self.wBounds - 10 - self.createMoxybutton.frame.size.wIDth - 20 - self.centerMapbutton.frame.size.wIDth - 20,searchController.searchbar.frame.height) self })} 问题解除Searchbar的图像
Gif动画从处于活动状态的searchbar开始,取消按钮可见
解决方法 我不能声称这产生超级流畅的动画,但它看起来并不可怕,可能是有用的(甚至是您需要的).如果你想尝试一下,你可以创建一个新的项目(单视图应用程序),只需添加导航控制器作为初始的控件,并将VIEwController类的对象作为其根控制器(只是为了快速获取导航栏).下面的代码是我的VIEwController的全部实现import UIKitclass VIEwController: UIVIEwController,UISearchbarDelegate,UISearchControllerDelegate,UISearchResultsUpdating { lazy var createMoxybutton:UIbarbuttonItem = { //customize this as your equire let button = UIbarbuttonItem(Title: "Done",style: .Plain,target: nil,action: nil) return button }() lazy var centerMapbutton:UIbarbuttonItem = { //customize this as your equire let button = UIbarbuttonItem(Title: "Done",action: nil) return button }() lazy var searchController:UISearchController = { let controller = UISearchController(searchResultsController: self) controller.delegate = self controller.searchResultsUpdater = self controller.dimsBackgroundDuringPresentation = false controller.hIDesNavigationbarDuringPresentation = false return controller }() overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. self.navigationItem.TitleVIEw = self.searchController.searchbar self.navigationItem.rightbarbuttonItems = [self.centerMapbutton,self.createMoxybutton] self.edgesForExtendedLayout = UIRectEdge.None self.searchController.searchbar.delegate = self } func updateSearchResultsForSearchController(searchController: UISearchController) { } func searchbarTextDIDBeginEditing(searchbar: UISearchbar) { //this needs to be done because in shouldEndEditing //we need to set it to false to smooth animation searchbar.showsCancelbutton = true self.navigationItem.setRightbarbuttonItems(nil,animated: true) } func searchbarShouldEndEditing(searchbar: UISearchbar) -> Bool { searchbar.showsCancelbutton = false self.navigationItem.rightbarbuttonItems = [self.centerMapbutton,self.createMoxybutton] return true }} 总结 以上是内存溢出为你收集整理的ios – 自定义UISearchController动画全部内容,希望文章能够帮你解决ios – 自定义UISearchController动画所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)