我对这个东西还很新。这个应用程序只是我的第二个。
我有一个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];
}
它第一次起作用,但随后出现了蛇眼。
我确信我在这里犯了一些基本的,愚蠢的错误,并且正在寻找线索。
线索,有人吗?
谢谢!
答案 0 :(得分:3)
哇。我解决了它,我不确定为什么,但它确实有效。
这就是我的所作所为:
首先,我将过渡分解为标准方法:
/***************************************************************\**
\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]];
有效。我用仪器检查了一下。没有泄漏。
答案 1 :(得分:2)
此代码工作正常 **
int controllerIndex = 2;
UIView * fromView = self.tabBarController .selectedViewController.view;
UIView * toView = [[self.tabBarController.viewControllers objectAtIndex:controllerIndex] view];
// Transition using a page curl.
[UIView transitionFromView:fromView toView:toView duration:0.5
options:(controllerIndex > self.tabBarController.selectedIndex ? UIViewAnimationOptionTransitionCurlUp : UIViewAnimationOptionTransitionCurlDown)
completion:^(BOOL finished) {
if (finished) {
self.tabBarController.selectedIndex = controllerIndex;
}
}];**