iPhone - presentModalViewController从右侧过渡

时间:2011-07-29 16:40:06

标签: iphone uiviewcontroller transition

我正在使用presentModalView控制器并通过其控制器将转换推送到新视图。 我正在使用以下代码进行转换(工作正常)

    [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"];  

但问题是,它显示空白屏幕然后转换开始。怎么避免这个?

3 个答案:

答案 0 :(得分:3)

首先添加动画然后出现

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];

答案 1 :(得分:1)

由于没有回答这个问题,我会发布对我有用的内容,让未来的搜索者找到有用的东西:

我试图在一个带有CATransition的不同XIB文件中显示一个视图,并且在开头总是有一个黑屏。

解决方案是更换这些线

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

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

找到问题后,很容易理解,因为显然本地窗口是你想要“推”的窗口。

答案 2 :(得分:0)

我认为,uiviewcontroller.transitioningDelegate是在iOS7和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())
        })
    }
}