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

Mor*_*Man 4 animation uitableview ios swift uisearchcontroller

轻触a时UISearchBar,取消按钮滑入,TextField位于上方以容纳按钮.我将这称为取消按钮动画.

在我的应用程序中,当用户点击搜索按钮时,带有a 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)
Run Code Online (Sandbox Code Playgroud)

当我将searchBar第一响应者关闭屏幕时,不应该完成取消按钮动画完成吗?它没有.

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

小智 7

这是来自UICatalog示例代码的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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 最好将其作为一个单独的问题发布,以便您可以显示新代码,我们可以看到您如何/在何处驳回演示文稿. (2认同)