2014-04-05 68 views

回答

18

@rounak有正確的想法,但有時它有助於準備好代碼而無需從github下載。

這裏是我採取的步驟:

  1. 讓您FromViewController.m符合UINavigationControllerDelegate。其他示例代碼告訴您要符合UIViewControllerTransitioningDelegate,但只有當您的呈現 ToViewController時。

    @interface ViewController中:UIViewController中

  2. 返回您的自定義動畫過渡對象委託回調方法FromViewController:

    - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController 
               animationControllerForOperation:(UINavigationControllerOperation)operation 
                  fromViewController:(UIViewController *)fromVC 
                   toViewController:(UIViewController *)toVC { 
        TransitionAnimator *animator = [TransitionAnimator new]; 
        animator.presenting = (operation == UINavigationControllerOperationPush); 
        return animator; 
    } 
    
  3. 創建自定義動畫類和粘貼這些樣品的方法:

    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { 
        return 0.5f; 
        } 
    
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext  { 
    // Grab the from and to view controllers from the context 
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
    
    // Set our ending frame. We'll modify this later if we have to 
    CGRect endFrame = CGRectMake(80, 280, 160, 100); 
    
    if (self.presenting) { 
        fromViewController.view.userInteractionEnabled = NO; 
    
        [transitionContext.containerView addSubview:fromViewController.view]; 
        [transitionContext.containerView addSubview:toViewController.view]; 
    
        CGRect startFrame = endFrame; 
        startFrame.origin.x += 320; 
    
        toViewController.view.frame = startFrame; 
    
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
         fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; 
         toViewController.view.frame = endFrame; 
        } completion:^(BOOL finished) { 
         [transitionContext completeTransition:YES]; 
        }]; 
    } 
    else { 
        toViewController.view.userInteractionEnabled = YES; 
    
        [transitionContext.containerView addSubview:toViewController.view]; 
        [transitionContext.containerView addSubview:fromViewController.view]; 
    
        endFrame.origin.x += 320; 
    
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
         toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; 
         fromViewController.view.frame = endFrame; 
        } completion:^(BOOL finished) { 
         [transitionContext completeTransition:YES]; 
        }]; 
    } 
    } 
    

本質上,動畫師是做繁重的對象。當然,你可以讓你的UINavigationControllerDelegate成爲一個單獨的對象,但這取決於你的架構應用程序的方式。

+0

這個工作適用於ios 8嗎? – SleepsOnNewspapers

+0

是的,它應該工作 – josephmisiti

1

編輯:剛纔意識到這可能不會回答你的問題。但它是一種選擇。

如果您正在使用故事板,則可以通過創建自定義細分來進行自定義轉換。 在屬性檢查器中,將segue類名稱更改爲您的自定義轉換類,例如MySegue。然後創建MySegue類並實施-(void)perform方法來執行轉換。

- (void) perform{ 
     UIViewController *source = self.sourceViewController; 
     UIViewController *destination = self.destinationViewController; 
     [UIView transitionFromView:source.view 
          toView:destination.view 
         duration:0.50f 
         options:UIViewAnimationOptionTransitionFlipFromTop 
         completion:nil]; 
} 
4

objc.io的視圖控制器轉換文章專門用於推送和彈出視圖控制器。 http://objc.io/issue-5/view-controller-transitions.html

我做這個動畫(http://i.imgur.com/1qEyMu3.gif)完全基於objc.io崗位。

總之,你必須有一個類實現UINavigationControllerDelegateUIViewControllerAnimatedTransitioning與返回正確的動畫師和執行動畫所需的方法。

+0

您可以分享動畫代碼嗎?我正在處理圖片中顯示的類似內容。 –

+0

我在push和pop(0.01,0)期間給CGAffineTransform(translationX:1.02,y:1.02)。01)似乎工作正常,想知道你的價值也是如此:) –

相關問題