iPhone - presentModalViewController with transitio

2019-01-26 01:15发布

问题:

I'm using presentModalView controller and pushing the transition to new view via its controller. And I'm using following code to do the transition (which is working fine)

    [self presentModalViewController:myViewController animated:NO];
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[myViewController.view layer] addAnimation:animation forKey:@"SwitchToView"];  

but the problem is that, its showing blank white screen and then transition starts. How to avoid this?

回答1:

first add animation then present

CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[myViewController.view layer] addAnimation:animation forKey:@"SwitchToView"];  


[self presentModalViewController:myViewController animated:NO];


回答2:

As this wasn't answered, I'll post what worked for me, to allow future seekers to find something useful:

I was trying to display a view in a different XIB file with a CATransition and always got a black screen at the beginning.

The solution was to replace these lines

[[myViewController.view layer];
addAnimation:animation forKey:@"SwitchToView"];

with

[[self.view.superview layer] addAnimation:animation forKey:@"SwitchToView"];

After finding the issue, it was quite easy to understand, because obviously the local window is the one you want to "push".



回答3:

I think, uiviewcontroller.transitioningDelegate is the way to do it on iOS7 ans 8 :

//Mark : Custom Transitionning for modal
func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, fromRight:Bool , completion: (() -> Void)?) {
    if fromRight
    {
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationStyle.FullScreen
        viewControllerToPresent.transitioningDelegate = self;
        self.presentViewController(viewControllerToPresent, animated: true, completion: completion);
    }
    else
    {
        self.presentViewController(viewControllerToPresent, animated: true, completion: completion)
    }
}


//MARK : Transitioning Delegate
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return self;
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return self;
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
    return 0.4;
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
    let toView = toViewController.viewForTransitionContext(transitionContext)
    let fromView = fromViewController.viewForTransitionContext(transitionContext)
    let containerView = transitionContext.containerView()
    let duration = self.transitionDuration(transitionContext);

    let initialFrame = transitionContext.initialFrameForViewController(fromViewController)
    var offsetRect = initialFrame
    offsetRect.origin.x += CGRectGetWidth(initialFrame);

    //Present
    if toViewController.isBeingPresented()
    {
        // init state before animation
        toView.frame = offsetRect;
        containerView.addSubview(toView);
        // then animate
        UIView.animateWithDuration(duration, animations: { () -> Void in
            toView.frame = initialFrame;
            }, completion: { (finished) -> Void in
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        })

    }
    //Dismiss
    else
    {
        // init state before animation
        containerView.addSubview(toView)
        containerView.sendSubviewToBack(toView)
        // then animate
        UIView.animateWithDuration(duration, animations: { () -> Void in
            fromView.frame = offsetRect
            }, completion: { (finished) -> Void in
                fromView.removeFromSuperview()
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        })
    }
}