ios – 自定义UISearchController动画

前端之家收集整理的这篇文章主要介绍了ios – 自定义UISearchController动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题:

如何覆盖退出属于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 UIKit

class 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
    }
}
原文链接:https://www.f2er.com/iOS/335816.html

猜你在找的iOS相关文章