UITabBarController语言 - 我如何动画简单的开关



在这方面我还是个新手。这个应用程序只是我的第二个。

我有一个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]];

。我和仪器部核对过了。没有泄漏。

这段代码运行正常 **

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;
                    }
                }];**

最新更新