这可能是一个奇怪的问题,但对于我想要实现的目标可能有不同的解决方案,我对任何需要的东西都持开放态度!
事实:
我有一个 UITableView
和一个由 UISearchDisplayController
处理的 UISearchBar
。
上面说TableView我有一些按钮和一个小的UIScrollView
。
当用户点击搜索栏并且键盘出现时,打字时显示搜索结果的空间很小。
因此,当用户开始搜索时,我想将 TableView 一直移动到顶部(覆盖按钮和 ScrollView)。
我的代码和问题:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
...
[UIView beginAnimations:@"Search" context:nil];
[UIView setAnimationDuration:0.6];
[self.attractionsTableView setFrame:CGRectMake(0, 0, 320, 400)];
[UIView commitAnimations];
...
return;
}
现在的问题是,由于我的 UITableView
必须移动的距离约为 100 点,SearchDisplayController 将黑色覆盖层放在 TableView 顶部时速度更快,因此黑色覆盖层已经打开顶部,而 TableView 仍在向上滑动。
结果是一个看起来像这样的屏幕:
problem http://img833.imageshack.us/img833/9186/problemqdu.png
因此,用户看到 TableView 向上滑动(这很好),但黑色覆盖层已经在那里等待,因为 searchDisplayController 的动画比我的快。
所以我有一个可能的想法,但不知道如何做到这一点:
有没有办法设置将黑色覆盖层放在搜索 tableView 顶部的 searchDisplayController 动画的持续时间?
编辑:
我知道我可以将animationDuration设置为0.01甚至0,但是对于100点的距离来说0.6的速度似乎相当不错,所以我试图找到一个不同的解决方案来降低持续时间。
最佳答案
诀窍是在动画完成之前不要让运行循环正常运行。当运行循环以某种模式运行时执行动画。幸运的是,覆盖动画是在与其他动画不同的运行循环模式下执行的,其他动画在默认运行循环模式下运行。因此,您所要做的就是从方法内运行运行循环,直到动画完成。
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
BOOL done = NO;
// Pass the done variable's address as the context so we can be notified
[UIView beginAnimations:@"SearchExpand" context:&done];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)];
[UIView setAnimationDuration:0.6];
// Expend the table view to fill its superview
self.attractionsTableView.frame = [[self.attractionsTableView superview] bounds];
[UIView commitAnimations];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// Run the run loop until the animation completes
while(!done) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(![runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])
break;
[pool drain];
}
}
- (void)animationDone:(NSString *)name finished:(NSNumber *)finished context:(void *)context {
// Set the done flag to YES
*((BOOL*)context) = YES;
// and kill the run loop
CFRunLoopStop(CFRunLoopGetCurrent());
}
关于ios - 让线程进入休眠状态但保持动画继续 - 这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590194/