2012-05-28 3 views
10

Я пытаюсь использовать пользовательский segue для выполнения своего рода анимации масштабирования. Когда переход выполняется, sourceViewController становится черным, затем происходит масштабирование.Custom Segue Animation

Пробовал также установить pushViewController: в блок завершения, но переход не выполняется вообще.

- (void)perform { 

    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController; 
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController; 

    [destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; 
    [destinationViewController.view setAlpha:0.0]; 

    [UIView animateWithDuration:0.5 
          delay:0.0 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)]; 
         [destinationViewController.view setAlpha:1.0]; 
         [sourceViewController.navigationController pushViewController:destinationViewController animated:NO]; 
        } 
        completion:^(BOOL finished){ 
        }]; 

} 

Что я делаю неправильно?

ответ

16

Он чувствует себя kludgy, но вы можете попробовать добавить destinationViewController.view в качестве подзаголовка перед вашей анимацией, а затем, когда анимация будет выполнена, удалите ее и верните обратно без анимации. Решает черный экран перед переходом, но, возможно, не идеально, в зависимости от того, что вы хотите сделать с панелью навигации, но, возможно, ближе:

[sourceViewController.view addSubview:destinationViewController.view]; 
[destinationViewController.view setFrame:sourceViewController.view.window.frame]; 
[destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; 
[destinationViewController.view setAlpha:1.0]; 

[UIView animateWithDuration:0.5 
         delay:0.0 
        options:UIViewAnimationCurveEaseOut 
       animations:^{ 
        [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)]; 
        [destinationViewController.view setAlpha:1.0]; 
       } 
       completion:^(BOOL finished){ 
        [destinationViewController.view removeFromSuperview]; 
        [sourceViewController.navigationController pushViewController:destinationViewController animated:NO]; 
       }]; 

Обратите внимание, эффективные IOS 7, можно использовать пользовательские переходы , Для получения дополнительной информации см. WWDC 2013 Custom Transitions Using View Controllers.

Например, если пытаться сделать пользовательский переход с навигационным контроллером, то первый контроллер представления будет указать себя в качестве делегата от навигационного контроллера:

self.navigationController.delegate = self; 

Тогда было бы указать пользовательские аниматор для толчка и поп, соответственно:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController 
            animationControllerForOperation:(UINavigationControllerOperation)operation 
               fromViewController:(UIViewController *)fromVC 
               toViewController:(UIViewController *)toVC 
{ 
    if (operation == UINavigationControllerOperationPush) { 
     return [[PushAnimator alloc] init]; 
    } else { 
     return [[PopAnimator alloc] init]; 
    } 
} 

и тогда вы, очевидно, реализовать эти аниматоры:

@interface PushAnimator : NSObject <UIViewControllerAnimatedTransitioning> 

@end 

@implementation PushAnimator 

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{ 
    return 0.5; 
} 

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext 
{ 
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 

    [[transitionContext containerView] addSubview:toViewController.view]; 
    toViewController.view.frame = fromViewController.view.frame; 
    toViewController.view.transform = CGAffineTransformMakeScale(0.5,0.5); 
    toViewController.view.alpha = 0.0; 

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{ 
     toViewController.view.transform = CGAffineTransformIdentity; 
     toViewController.view.alpha = 1.0; 
    } completion:^(BOOL finished) { 
     [fromViewController.view removeFromSuperview]; 
     [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
    }]; 
} 

@end 

и

@interface PopAnimator : NSObject <UIViewControllerAnimatedTransitioning> 

@end 

@implementation PopAnimator 

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{ 
    return 0.5; 
} 

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext 
{ 
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 

    [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view]; 
    toViewController.view.frame = fromViewController.view.frame; 

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{ 
     fromViewController.view.transform = CGAffineTransformMakeScale(0.5,0.5); 
     fromViewController.view.alpha = 0.0; 
    } completion:^(BOOL finished) { 
     [fromViewController.view removeFromSuperview]; 
     [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
    }]; 
} 

@end 
+0

спасибо, что он работает отлично! – Nimrod7

 Смежные вопросы

  • Нет связанных вопросов^_^