【发布时间】:2023-03-27 21:40:01
【问题描述】:
我有一个 UITabBarController,显示 4 个标签。
我想在用户滑动屏幕切换选项卡时添加动画 UIViewController 过渡。 (- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController 方法仅适用于直接选择选项卡)。过渡将采用easeinout 风格。
我尝试了以下代码但不起作用。只有来的 UIViewController 移动,去的 UIViewController 根本不显示。(我打印了所有 UIViewControllers 的帧数据,所有这 4 个 UIViewControllers 都是 {0,0},{320,480})
// Get views. controllerIndex is passed.
UIViewController *fromVC = [_tabBarController.viewControllers objectAtIndex:startIndex];
UIViewController *toVC = [_tabBarController.viewControllers objectAtIndex:to];
UIView *fromView = fromVC.view;
UIView *toView = toVC.view;
MWLog(@"from view is %@",fromView);
int direct = startIndex - to;
// calculate move direction.
if (direct < 0) {
CGRect outFrame = fromView.frame;
outFrame.origin.x = -320; // expect fromView will move to {-320,0}, out of screen.
MWLog(@"fromView's frame is %@", NSStringFromCGRect(fromView.frame));
CGRect inFrame = toView.frame;
inFrame.origin.x = 0;
MWLog(@"toView's frame is %@", NSStringFromCGRect(toView.frame));
[UIView beginAnimations:@"moveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5f];
toView.frame = inFrame;
fromView.frame = outFrame;
[UIView commitAnimations];
}else
{
// reverse moving. ignored....
}
您能告诉我哪里出了问题以及如何正确处理吗?提前致谢!
【问题讨论】:
-
我终于找到了解决方案。使用 CATransition 动画,键入 push,根据方向键入 left 或 right(通过计算 tabbarcontroller 的 selectedIndex 属性)。然后,将此动画添加到 tabbarcontroller 的容器视图。这个视图的引用可以通过枚举 [tabbarcontroller subviews] 数组中的所有 UIViews 来获得。实际上,如果没有自定义 UIView,tabbarcontroller 包含 2 个子视图,一个是 UITabBar,另一个是容器视图 UITransitionView。
-
我不明白你的解决方案的每一个字。如果您可以发布更正的示例,那就太好了。谢谢!
标签: animation uitabbarcontroller transition