首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iPhone:如何在动画中切换选项卡?

iPhone:如何在动画中切换选项卡?
EN

Stack Overflow用户
提问于 2011-03-02 07:33:28
回答 19查看 56.8K关注 0票数 108

我使用UITabBarController.selectedIndex在标签栏驱动的应用程序中以编程方式切换标签。我试图解决的问题是如何使视图之间的过渡具有动画效果。即。从当前选项卡的视图到选定选项卡的视图。

最初的想法是使用UITabBarControllerDelegate,但似乎在以编程方式切换选项卡时没有调用它。我现在考虑将UITabBarDelegate.didSelectItem:作为设置过渡动画的一个可能的钩子。

有没有人成功地制作了这些过渡的动画?如果是,是如何实现的?

EN

回答 19

Stack Overflow用户

回答已采纳

发布于 2011-03-03 19:26:57

2016年4月4日更新: Justed想要更新这篇文章,感谢大家的投票。还请注意,这最初是在很久以前写的……在ARC之前,在约束之前,在...很多东西!因此,在决定是否使用这些技术时,请考虑这一点。可能会有更现代的方法。哦,如果你找到了。请添加回复,以便每个人都能看到。谢谢。

一段时间后的 ...

经过大量的研究,我想出了两个可行的解决方案。这两种方法都有效,并在选项卡之间实现了动画效果。

解决方案1:从视图(简单)过渡

这是最简单的方法,并且使用了预定义的UIView转换方法。使用这个解决方案,我们不需要管理视图,因为方法会为我们做这些工作。

代码语言:javascript
运行
复制
// Get views. controllerIndex is passed in as the controller we want to go to. 
UIView * fromView = tabBarController.selectedViewController.view;
UIView * toView = [[tabBarController.viewControllers objectAtIndex:controllerIndex] view];

// Transition using a page curl.
[UIView transitionFromView:fromView 
                    toView:toView 
                  duration:0.5 
                   options:(controllerIndex > tabBarController.selectedIndex ? UIViewAnimationOptionTransitionCurlUp : UIViewAnimationOptionTransitionCurlDown)
                completion:^(BOOL finished) {
                    if (finished) {
                        tabBarController.selectedIndex = controllerIndex;
                    }
                }];

解决方案2:滚动(更复杂)

更复杂的解决方案,但使您可以更好地控制动画。在这个例子中,我们得到了打开和关闭的视图。使用这个,我们需要自己管理视图。

代码语言:javascript
运行
复制
// Get the views.
UIView * fromView = tabBarController.selectedViewController.view;
UIView * toView = [[tabBarController.viewControllers objectAtIndex:controllerIndex] view];

// Get the size of the view area.
CGRect viewSize = fromView.frame;
BOOL scrollRight = controllerIndex > tabBarController.selectedIndex;

// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];

// Position it off screen.
toView.frame = CGRectMake((scrollRight ? 320 : -320), viewSize.origin.y, 320, viewSize.size.height);

[UIView animateWithDuration:0.3 
                 animations: ^{

                     // Animate the views on and off the screen. This will appear to slide.
                     fromView.frame =CGRectMake((scrollRight ? -320 : 320), viewSize.origin.y, 320, viewSize.size.height);
                     toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
                 }

                 completion:^(BOOL finished) {
                     if (finished) {

                         // Remove the old view from the tabbar view.
                         [fromView removeFromSuperview];
                         tabBarController.selectedIndex = controllerIndex;                
                     }
                 }];

Swift中的此解决方案:

代码语言:javascript
运行
复制
extension TabViewController: UITabBarControllerDelegate {
      public func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

           let fromView: UIView = tabBarController.selectedViewController!.view
           let toView  : UIView = viewController.view
           if fromView == toView {
                 return false
           }

           UIView.transitionFromView(fromView, toView: toView, duration: 0.3, options: UIViewAnimationOptions.TransitionCrossDissolve) { (finished:Bool) in

        }
        return true
   }
}
票数 157
EN

Stack Overflow用户

发布于 2012-11-29 12:22:19

下面是我尝试将代码形式drekka用于委托(UITabBarControllerDelegate)方法

代码语言:javascript
运行
复制
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSArray *tabViewControllers = tabBarController.viewControllers;
    UIView * fromView = tabBarController.selectedViewController.view;
    UIView * toView = viewController.view;
    if (fromView == toView)
        return false;
    NSUInteger fromIndex = [tabViewControllers indexOfObject:tabBarController.selectedViewController];
    NSUInteger toIndex = [tabViewControllers indexOfObject:viewController];

    [UIView transitionFromView:fromView
                        toView:toView
                      duration:0.3
                       options: toIndex > fromIndex ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
                    completion:^(BOOL finished) {
                        if (finished) {
                            tabBarController.selectedIndex = toIndex;
                        }
                    }];
    return true;
}
票数 25
EN

Stack Overflow用户

发布于 2015-08-18 14:07:22

我的iOS7.0或更高版本的解决方案。

可以在选项卡栏的代理中指定自定义动画控制器。

实现动画控制器,如下所示:

代码语言:javascript
运行
复制
@interface TabSwitchAnimationController : NSObject <UIViewControllerAnimatedTransitioning>

@end

@implementation TabSwitchAnimationController

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.2;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView* toView = toVC.view;
    UIView* fromView = fromVC.view;

    UIView* containerView = [transitionContext containerView];
    [containerView addSubview:toView];
    toView.frame = [transitionContext finalFrameForViewController:toVC];

    // Animate by fading
    toView.alpha = 0.0;
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         toView.alpha = 1.0;
                     }
                     completion:^(BOOL finished) {
                         toView.alpha = 1.0;
                         [fromView removeFromSuperview];
                         [transitionContext completeTransition:YES];
                     }];
}

@end

然后在你的UITabBarControllerDelegate中使用它:

代码语言:javascript
运行
复制
- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC
{
    return [[TabSwitchAnimationController alloc] init];
}
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5161730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档