1

我在“UISearchDisplayDelegate 协议参考”(SearchBar-animated-sample )下找到了带有范围栏的动画搜索栏示例,这是一个视频预览: SearchBarAnimated-video

我检查了示例代码,但找不到触发动画的代码。有谁知道如何创建该动画?你必须使用 UISearchBarDelegate 来获得那个动画吗?

4

5 回答 5

1

为了控制 UISearchBar 的动画,您已经通过在头文件中扩展来实现 UISearchDisplayController 的委托。代表如下;

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        [UIView beginAnimations:nil context:NULL];
        self.searchDisplayController.searchBar.showsScopeBar = NO;
        CGRect headerViewFrame = self.searchDisplayController.searchBar.frame;
        headerViewFrame.origin.y -= 54.0f;
        self.searchDisplayController.searchBar.frame = headerViewFrame;

        CGRect tableViewFrame = self.tableView.frame;
        tableViewFrame.origin.y -= 54.0f;
        self.tableView.frame = tableViewFrame;

        [UIView commitAnimations];
    }

    -(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
    {
        [UIView beginAnimations:nil context:NULL];

        CGRect headerViewFrame = self.searchDisplayController.searchBar.frame;
        headerViewFrame.origin.y += 54.0f;
        self.searchDisplayController.searchBar.frame = headerViewFrame;

        CGRect tableViewFrame = self.tableView.frame;
        tableViewFrame.origin.y += 54.0f;
        self.tableView.frame = tableViewFrame;

        [UIView commitAnimations];
    }
于 2014-04-08T08:23:03.810 回答
0

这在 Xcode 6 中对我很有效。如果您有自动布局约束,那么您可能需要像我所做的那样为它们添加调整(没有它们就无法工作)。

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsScopeBar = YES;
    searchBarHeightConstraint.constant = 88; // Changes from 44 to 88 with scope bar
    tableViewHeightConstraint.constant = 480; // Changes from 524 to 480 with scope bar

    [UIView animateWithDuration:0.3
                     animations:^{
                         CGRect newFrame = tableView.frame;
                         newFrame.origin.y = 88;
                         tableView.frame = newFrame;
                     }];

    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsScopeBar = NO;
    searchBarHeightConstraint.constant = 44;
    tableViewHeightConstraint.constant = 524;

    [UIView animateWithDuration:0.3
                     animations:^{
                         CGRect newFrame = tableView.frame;
                         newFrame.origin.y = 44;
                         tableView.frame = newFrame;
                     }];
    return YES;
}
于 2014-10-15T03:19:11.547 回答
0

它内置于UISearchBar. 苹果为你做这件事,你不必自己调用任何方法。

基本上,从您设置搜索栏scopeButtonTitles属性的那一刻起,Apple 就会为范围栏设置动画。

于 2011-09-25T11:09:30.040 回答
0

我发现这个问题的答案更有用,尽管它不会自动将搜索栏转换到您的视图顶部。

你如何用动画隐藏/显示 UISearchBar 的范围栏?

于 2012-05-11T06:09:39.997 回答
0

动画块中的 sizeToFit

UISearchBarw/范围栏很容易动画。UISearchBar 在使用范围栏调用 sizeToFit 之前的高度为 44.f,然后变为 88.f。就我而言,它UISearchBar嵌入在UITableViewInterface Builder 中,因此无法添加自动布局约束。

#pragma mark - UISearchBarDelegate methods

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsScopeBar = YES;
    [UIView animateWithDuration:0.2f animations:^{
        [searchBar sizeToFit];
    }];

    [searchBar setShowsCancelButton:YES animated:YES];

    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsScopeBar = NO;
    [searchBar sizeToFit];

    [searchBar setShowsCancelButton:NO animated:YES];

    return YES;
}
于 2015-06-06T08:43:52.530 回答