我在这个东西上还很新。这个程序只是我的第二个。
我有一个TabBarController,视图之间有两种切换方式:
方法1:使用选项卡栏图标。我提供了必需的委托回调。它是这样的:
/***************************************************************\**
\brief This animates the view transitions, and also sets up anything
that needs doing between views.
*****************************************************************/
- (BOOL)tabBarController:(UITabBarController *)inTabBarController
shouldSelectViewController:(UIViewController *)inViewController
{
BOOL ret = NO;
// Need to have all of these to work.
if ( inTabBarController && [inTabBarController view] && inViewController && [inViewController view] )
{
UIView *srcView = [[inTabBarController selectedViewController] view];
UIView *dstView = [inViewController view];
if ( srcView != dstView )
{
if ( srcView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlDown
completion:nil];
}
else if ( dstView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlUp
completion:nil];
}
else if ( srcView == [listSearchController view] && dstView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
else if ( dstView == [listSearchController view] && srcView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
}
ret = YES;
}
}
return ret;
}
效果很好。
方法2:抓取滑动,并以此方式触发转换,如下所示:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[tabBarController setSelectedIndex:1];
}
问题是我无法过渡到工作。如果我将转换代码添加到滑动处理程序中,如下所示:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[UIView transitionFromView:[listSearchController view]
toView:[mapSearchController view]
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
[tabBarController setSelectedIndex:1];
}
它是第一次工作,但随后又出现了蛇眼。
我确定我在这里犯了一些基本的,轻笑的错误,并且正在寻找线索。
线索,有人吗?
谢谢!
最佳答案
哇。我解决了这个问题,但我不确定原因为何,但是可以解决。
这是我所做的:
首先,我将转换因素分解为标准方法:
/***************************************************************\**
\brief Manages the transition from one view to another. Just like
it says on the tin.
*****************************************************************/
- (void)transitionBetweenThisView:(UIView *)srcView
andThisView:(UIView *)dstView
{
if ( srcView != dstView )
{
if ( srcView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlDown
completion:nil];
}
else if ( dstView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlUp
completion:nil];
}
else if ( srcView == [listSearchController view] && dstView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
else if ( dstView == [listSearchController view] && srcView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
}
}
}
接下来,我在制表符栏过渡发生之前对其进行拦截,并将其覆盖,如下所示:
/***************************************************************\**
\brief This animates the view transitions, and also sets up anything
that needs doing between views. It stops the tab bar controller
from managing the transition, and does it manually.
\returns a BOOL. Always NO.
*****************************************************************/
- (BOOL)tabBarController:(UITabBarController *)inTabBarController
shouldSelectViewController:(UIViewController *)inViewController
{
[self transitionBetweenThisView:[[inTabBarController selectedViewController] view] andThisView:[inViewController view]];
int index = [[inTabBarController viewControllers] indexOfObject:inViewController];
[inTabBarController setSelectedIndex:index];
return NO;
}
然后,将以下代码添加到滑动陷阱中:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[self transitionBetweenThisView:[listSearchController view] andThisView:[mapSearchController view]];
[tabBarController setSelectedIndex:1];
}
现在,这里的问题是,当我返回刷过的视图(不使用标签栏)时,总是会崩溃。飞机坠毁表明目标视图已被重新分配。
在检查了一些鸡肉内脏之后,我决定需要握住鼻子,并保留以前的视图,如下所示:
[[listSearchController view] retain];
[self transitionBetweenThisView:[listSearchController view] andThisView:[mapSearchController view]];
这样可行。我用仪器检查过。没有泄漏。
关于iphone - UITabBarController-如何为简单的开关设置动画?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7694476/