亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

iPhone:如何用動畫切換標簽?

iPhone:如何用動畫切換標簽?

iOS
慕姐8265434 2020-02-04 15:54:01
我正在使用標簽欄驅動的應用程序中以編程方式切換標簽UITabBarController.selectedIndex。我要解決的問題是如何為視圖之間的過渡設置動畫。即。從當前選項卡的視圖到所選選項卡的視圖。首先想到的是利用UITabBarControllerDelegate,但似乎在以編程方式切換選項卡時未調用此方法。我現在考慮使用UITabBarDelegate.didSelectItem:作為設置過渡動畫的可能鉤子。有沒有人設法使過渡動畫?如果是,怎么辦?
查看完整描述

3 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

更新04/2016: Justed想更新此內容,對所有人的所有投票表示感謝。另請注意,這最初是在...之前,ARC之前,約束之前,...之前寫的。因此,在決定是否使用這些技術時,請考慮到這一點??赡軙懈喱F代方法。哦,如果找到一個。請添加回復,以便所有人都能看到。謝謝。


一段時間之后 ...


經過大量研究,我提出了兩個可行的解決方案。這兩個選項在標簽之間都起作用并且起作用。


解決方案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

   }

}


查看完整回答
反對 回復 2020-02-04
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

以下是我嘗試使用代碼形式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;

}


查看完整回答
反對 回復 2020-02-04
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

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


您可以在標簽欄的委托中指定自定義動畫控制器。


實現這樣的動畫控制器:


@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中使用它:


- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController

            animationControllerForTransitionFromViewController:(UIViewController *)fromVC

                                              toViewController:(UIViewController *)toVC

{

    return [[TabSwitchAnimationController alloc] init];

}


查看完整回答
反對 回復 2020-02-04
  • 3 回答
  • 0 關注
  • 908 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號