15

底线问题:

如何覆盖关闭属于 UISearchController 的 searchBar 的默认动画?


标准搜索控制器行为:

好的,所以我正在尝试为附加到 UISearchController 的 UISearchBar 变为活动状态时创建自定义动画。似乎标准动画期望 searchBar 以占据屏幕的宽度开始。当动画开始时,它会缩小 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, searchController.searchBar.frame.origin.y, self.wBounds - 10 - self.createMoxyButton.frame.size.width - 20 - self.centerMapButton.frame.size.width - 20, searchController.searchBar.frame.height)
        self

    })
}

关闭 SearchBar 问题的图像

Gif 动画从处于 Active 状态的 searchBar 开始,并且取消按钮可见

Gif 动画从处于 Active 状态的 searchBar 开始,并且取消按钮可见

4

2 回答 2

3

子类 UISearchController 并实现可选UIViewControllerTransitioningDelegate 方法 - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;

返回您的动画对象以执行解雇动画。

于 2016-04-28T21:25:39.400 回答
2

我不能声称这会产生超级流畅的动画,但它看起来并不可怕并且可能会有所帮助(甚至是您所需要的)。如果您想尝试一下,您可以创建一个新项目(单视图应用程序),然后将导航控制器添加为初始控制器,并将类的对象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", style: .Plain, target: nil, 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
    }
}
于 2015-06-25T01:37:49.003 回答