用户在屏幕外点击 UISearchBar 的完整动画

Complete animation of user tapping UISearchBar off screen

点击 UISearchBar 时,取消按钮滑入并且 TextField 位于上方以容纳该按钮。我将其称为取消按钮动画。

在我的应用程序中,当用户点击搜索按钮时,带有 UISearchControllerUITableView 的视图淡入。我希望视图通过取消按钮动画淡入已经完成了。类似于 iOS 音乐应用。我试过这个:

self.myTableView.frame = CGRectMake(0, 20, self.view.bounds.width, self.view.bounds.height - 20)
self.resultSearchController.searchBar.becomeFirstResponder()

UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: {
    self.myTableView.alpha = CGFloat(1.0)

        }, completion: nil)

当我将 searchBar 的第一响应者设置为不在屏幕上时,是否应该完成取消按钮动画?它没有。

现在,取消按钮动画发生在屏幕上,然后视图 (TableView) 淡入。如何在视图的 alpha 为 0 时发生取消按钮动画?

这是来自 UICatalog sample code 的 Apple Swift 版本。

它会在导航栏上方设置一个搜索控制器,就像音乐和日历一样。

正如您在该项目中看到的那样,在动画期间,searchBar 取消按钮已经可见,如您所愿。

当用户点击您的搜索按钮时,只需调用类似的代码,如本示例的 IBAction 所示。

/*
    Copyright (C) 2015 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information

    Abstract:
    A view controller that demonstrates how to present a search controller over a navigation bar.
*/

import UIKit

class SearchPresentOverNavigationBarViewController: SearchControllerBaseViewController {
    // MARK: Properties

    // `searchController` is set when the search button is clicked.
    var searchController: UISearchController!

    // MARK: Actions

    @IBAction func searchButtonClicked(button: UIBarButtonItem) {
        // Create the search results view controller and use it for the UISearchController.
        let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier(SearchResultsViewController.StoryboardConstants.identifier) as! SearchResultsViewController

        // Create the search controller and make it perform the results updating.
        searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.hidesNavigationBarDuringPresentation = false

       // Present the view controller.
        presentViewController(searchController, animated: true, completion: nil)
    }
}