2017-02-28 2 views
0

Ich versuche, UIViewControllerContextTransitioning arbeiten zu lassen.Benutzerdefinierte Modal Transition funktioniert nicht - Beispiel

Was ich will:

Was ich haben möchte, Modal-View-Controller mit benutzerdefinierten Animationen und transparentem Hintergrund präsentiert.

Wthat ich getan habe:

I erstellt Animator Umsetzung UIViewControllerTransitioningDelegate,

Set für modal Controller:

self.modalPresentationStyle = UIModalPresentationCustom; 
self.transitioningDelegate = self; 

Was ich bisher erreicht,

Modal-Controller animiert das Präsentieren und Dis Fehlende Sicht richtig, aber nachdem die Kündigung beendet ist, wird die gesamte App schwarz. Ich benutzte xCode-Tool, um auszuwählen, was in der Fensterhierarchie ist, und da ist nichts. Meine Vermutung ist, dass ich die Superansicht von VC geändert habe, als ich sie dem Context-Container hinzugefügt habe.

Animator

@implementation AlertAnimator 

const static CGFloat kAnimationDuration = 1.2; 

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

    if (self.transitionType == ModalAnimatedTransitioningTypePresent) { 
     [self animatePresentingInContext:transitionContext toVC:to fromVC:from]; 
    } else if (self.transitionType == ModalAnimatedTransitioningTypeDismiss) { 
     [self animateDismissingInContext:transitionContext toVC:to fromVC:from]; 
    } 
} 

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

- (void)animatePresentingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC { 
    CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC]; 
    CGRect toVCRect = fromVCRect; 
    toVCRect.origin.y = toVCRect.size.height; 

    toVC.view.frame = toVCRect; 
    UIView *container = [transitionContext containerView]; 
    [container addSubview:fromVC.view]; 
    [container addSubview:toVC.view]; 

    [UIView animateWithDuration:kAnimationDuration animations:^{ 
     toVC.view.frame = fromVCRect; 
    } completion:^(BOOL finished) { 
     [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
    }]; 
} 

- (void)animateDismissingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC { 
    CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC]; 
    fromVCRect.origin.y = fromVCRect.size.height; 

    UIView *container = [transitionContext containerView]; 
    [container addSubview:toVC.view]; 
    [container addSubview:fromVC.view]; 

    [UIView animateWithDuration:kAnimationDuration animations:^{ 
     fromVC.view.frame = fromVCRect; 
    } completion:^(BOOL finished) { 
     [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
    }]; 
} 

@end 

Code-Beispiel:

https://www.dropbox.com/s/oaghtgwvga4nxs4/Test.zip?dl=0

Frage:

Was ich falsch mache und warum wird Bildschirm schwarz?

+0

Wenn es sich um einen präsentierenden Animator handelt, sollten Sie die fromVC.view nicht als Unteransicht hinzufügen, da sie bereits vorhanden ist. Dies wird dazu führen, dass es ausfällt. Probieren Sie es aus und sagen Sie mir, was passiert – DatForis

+0

danke @DatForis, ich habe auskommentiert [container addSubview: fromVC.view]; 'in' animatePresenttingInContext: toVC: fromVC: 'aber nichts hat sich wirklich geändert. irgendeine andere Idee? – konradholub

+1

Entfernen von '[container addSubview: toVC.view];' von 'animatePresenttingInContext: toVC: fromVC:' hat funktioniert. Danke, dass du mich dorthin geführt hast. Bitte fügen Sie es als Antwort hinzu, damit ich es als gelöst markieren kann. – konradholub

Antwort

0

Wenn es sich um einen präsentierenden Animator handelt, sollten Sie die Datei fromVC.view nicht als Unteransicht hinzufügen, da sie bereits vorhanden ist. Dies wird dazu führen, dass es ausfällt. Probieren Sie es aus und sagen Sie mir, was passiert. Wenn es sich um einen Entlassungs-Animator handelt, sollten Sie auch den presentingViewController nicht zur Hierarchie hinzufügen.

Verwandte Themen