iPhone:如何用动画切换标签?

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

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

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


慕姐8265434
浏览 829回答 3
3回答

米琪卡哇伊

更新04/2016: Justed想更新此内容,对所有人的所有投票表示感谢。另请注意,这最初是在...之前,ARC之前,约束之前,...之前写的。因此,在决定是否使用这些技术时,请考虑到这一点。可能会有更多现代方法。哦,如果找到一个。请添加回复,以便所有人都能看到。谢谢。一段时间之后 ...经过大量研究,我提出了两个可行的解决方案。这两个选项在标签之间都起作用并且起作用。解决方案1:从视图过渡(简单)这是最简单的方法,并且使用了预定义的UIView过渡方法。使用此解决方案,我们不需要管理视图,因为该方法可以为我们完成工作。// 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:滚动(更复杂)一个更复杂的解决方案,但可以让您更好地控制动画。在此示例中,我们可以打开和关闭视图。对于这一点,我们需要自己管理视图。// 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中的此解决方案: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   }}

明月笑刀无情

以下是我尝试使用代码形式drekka到委托(UITabBarControllerDelegate)方法中- (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;}

拉丁的传说

我的iOS7.0或更高版本的解决方案。您可以在标签栏的委托中指定自定义动画控制器。实现这样的动画控制器:@interface TabSwitchAnimationController : NSObject <UIViewControllerAnimatedTransitioning>@end@implementation TabSwitchAnimationController- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{&nbsp; &nbsp; return 0.2;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{&nbsp; &nbsp; UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];&nbsp; &nbsp; UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];&nbsp; &nbsp; UIView* toView = toVC.view;&nbsp; &nbsp; UIView* fromView = fromVC.view;&nbsp; &nbsp; UIView* containerView = [transitionContext containerView];&nbsp; &nbsp; [containerView addSubview:toView];&nbsp; &nbsp; toView.frame = [transitionContext finalFrameForViewController:toVC];&nbsp; &nbsp; // Animate by fading&nbsp; &nbsp; toView.alpha = 0.0;&nbsp; &nbsp; [UIView animateWithDuration:[self transitionDuration:transitionContext]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delay:0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;animations:^{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toView.alpha = 1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;completion:^(BOOL finished) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toView.alpha = 1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[fromView removeFromSuperview];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[transitionContext completeTransition:YES];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}];}@end然后在您的UITabBarControllerDelegate中使用它:- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; animationControllerForTransitionFromViewController:(UIViewController *)fromVC&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toViewController:(UIViewController *)toVC{&nbsp; &nbsp; return [[TabSwitchAnimationController alloc] init];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS