自定义UIViewController动画没有UINAVIGATIONCONTROLLER

时间:2014-10-17 01:56:52

标签: objective-c animation uiviewcontroller

我有一个摄像机视图控制器。我想推它。但我希望它在我推动时淡入。我该怎么做呢。我没有使用UINavigationController。

1 个答案:

答案 0 :(得分:2)

你"推"导航堆栈上的视图控制器。但是当你展示一个没有导航控制器的视图控制器时,你可以将其命名为“#34; present" (不是"推")。意思是指推动"视图控制器很难连接到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];
}