2014-10-17 82 views

回答

2

您將視圖控制器「推入」導航堆棧。但是,當您呈現沒有導航控制器的視圖控制器時,您將其命名爲「present」(而不是「push」)。意味着術語「推」視圖控制器很難連接到UINavigationController

所以你可以只是谷歌的東西像「UIViewController自定義過渡」或類似的東西。

I did great success following a this great article by Teehan+Lax。只要讀它,它應該引導你正確的方式。

編輯好的。我會添加一些信息爲您提供更好的答案。

首先,您將創建的NSObject一個新的子類,符合UIViewControllerAnimatedTransitioning協議:

@interface JWTransitionAnimator <UIViewControllerAnimatedTransitioning> 
+ (JWTransitionAnimator *)transitionAnimatorForPresentation; 
+ (JWTransitionAnimator *)transitionAnimatorForDismissal; 
@end 

然後你讓你實現魔術發生:

@interface JWTransitionAnimator() 
@property (atomic, assign, getter=isPresenting) BOOL presenting; 
@end 

@implementation JWTransitionAnimator 
+ (JWTransitionAnimator *)transitionAnimatorForPresentation { 
    JWTransitionAnimator *animator = [[self alloc] init]; 
    [animator setPresenting:YES]; 

    return animator; 
} 
+ (JWTransitionAnimator *)transitionAnimatorForDismissal { 
    return [[self alloc] init]; 
} 

// implement the protocol: 
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitioningContext { 
    return 0.3; // or any other duration. 
} 
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitioningContext { 
    UIViewController *fromViewController = [transitioningContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 
    UIViewController *toViewController = [transitionContext UITransitionContextToViewController]; 

    UIView *fromView = [fromViewController view]; 
    UIView *toView = [toViewController view]; 

    if ([self isPresenting]) { 
     [fromView setUserInteractionEnabled:NO]; 

     [[transitionContext containerView] addSubview:fromView]; 
     [[transitionContext containerView] addSubview:toView]; 

     [toView setAlpha:0.0]; 
     [toView setTransform:CGAffineTransformScale([fromView transform], 0.3, 0.3)]; 

     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
      [fromView setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed]; 

      [toView setAlpha:1.0]; 
      [toView setTransform:CGAffineTransformScale([toView transform], 1.0, 1.0)]; 
     } completion:^(BOOL finished) { 
      [transitionContext completeTransition:finished]; 
     }]; 
    } 
    else { 
     // reverse your animation here (change "fromView" for "toView" and vice-versa) 
    } 
} 

您呈現viewController必須符合UIViewControllerTransitionDelegate協議。實現它如下:

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source { 
    return [JWTransitionAnimator transitionAnimatorForPresentation]; 
} 
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed { 
    return [JWTransitionAnimator transitionAnimatorForDismissal]; 
} 

在你目前的視圖控制器是這樣的:

- (IBAction)presentViewController:(id)sender { 
    UIViewController *yourVC = [[UIViewController alloc] init]; // however you create it 
    [yourVC setModalPresentationStyle:UIModalPresentationStyleCustom]; 
    [yourVC setTransitionDelegate:self]; 

    [self presentViewController:yourVC animated:YES completion:NULL]; 
} 
相關問題