【发布时间】:2011-09-25 11:01:38
【问题描述】:
我在“UISearchDisplayDelegate 协议参考”(SearchBar-animated-sample ),这是一个视频预览: SearchBarAnimated-video
我检查了示例代码,但找不到触发动画的代码。 有谁知道如何创建该动画?您是否必须使用 UISearchBarDelegate 来获取该动画?
【问题讨论】:
标签: iphone ios uisearchbar animated
我在“UISearchDisplayDelegate 协议参考”(SearchBar-animated-sample ),这是一个视频预览: SearchBarAnimated-video
我检查了示例代码,但找不到触发动画的代码。 有谁知道如何创建该动画?您是否必须使用 UISearchBarDelegate 来获取该动画?
【问题讨论】:
标签: iphone ios uisearchbar animated
为了控制 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];
}
【讨论】:
它内置于UISearchBar。苹果为你做这件事,你不必自己调用任何方法。
基本上,从您设置搜索栏的scopeButtonTitles 属性的那一刻起,Apple 就会为范围栏设置动画。
【讨论】:
我发现这个问题的答案更有用,尽管它不会自动将搜索栏转换到您的视图顶部。
How do you hide/show UISearchBar's scope bar with animation?
【讨论】:
这对我在 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;
}
【讨论】:
UISearchBar 带有范围栏很容易动画。 UISearchBar 在使用范围栏调用 sizeToFit 之前的高度为 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;
}
【讨论】: