UISearchBar - 范围栏动画

时间:2011-09-25 11:01:38

标签: iphone ios uisearchbar animated

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

我已经检查了示例代码,但是我找不到触发动画的代码。 有谁知道如何创建该动画?您是否必须使用UISearchBarDelegate来获取该动画?

5 个答案:

答案 0 :(得分: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];
    }

答案 1 :(得分:0)

它内置于UISearchBar。 Apple为您完成此操作,您无需亲自调用任何方法。

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

答案 2 :(得分:0)

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

How do you hide/show UISearchBar's scope bar with animation?

答案 3 :(得分: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;
}

答案 4 :(得分:0)

动画块中的sizeToFit

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

#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;
}