iPhone:如何用动画切换标签? [英] iPhone: How to switch tabs with an animation?

查看:96
本文介绍了iPhone:如何用动画切换标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm switching tabs programmatically in a tab bar driven application using UITabBarController.selectedIndex. The problem I'm trying to solve is how to animate the transition between the views. ie. from the view of the current tab to the view of the selected tab.

首先想到的是使用 UITabBarControllerDelegate ,但似乎在以编程方式切换选项卡时不会调用它。我现在正在考虑 UITabBarDelegate.didSelectItem :作为设置过渡动画的可能钩子。

The first thought was to make use of the UITabBarControllerDelegate, but it appears that this is not called when programmatically switching tabs. I'm now considering the UITabBarDelegate.didSelectItem: as a possible hook to set a transition animation.

有没有人设法使过渡动画?
如果是,怎么做?

Has anyone managed to animate the transitions? If yes, how ?

推荐答案

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

Update 04/2016: Justed wanted to update this to say thank you to everyone for all the votes. Please also note that this was originally written way back when ... before ARC, before constraints, before ... a lot of stuff! So please take this into account when deciding whether to use these techniques. There may be more modern approaches. Oh, and if you find one. Please add a response so everyone can see. Thanks.

一段时间后......

之后很多研究我想出了两个有效的解决方案。这两个都工作并在标签之间进行动画。

After much research I came up with two working solutions. Both of these worked and did the animation between tabs.

解决方案1:从视图转换(简单)

这是最简单的并且使用预定义的UIView转换方法。使用此解决方案,我们无需管理视图,因为该方法可以为我们工作。

This is the easiest and makes use of a predefined UIView transition method. With this solution we don't need to manage the views because the method does the work for us.

// 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:滚动(更复杂) )

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

A more complex solution, but gives you more control of the animation. In this example we get the views to slide on and off. With this one we need to manage the views ourselves.

// 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中的此解决方案:

This Solution in Swift:

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
   }
}

这篇关于iPhone:如何用动画切换标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆