ios swift自定义过渡到来自ViewController的变暗

时间:2015-08-13 09:05:38

标签: ios custom-transition

我想向我的视图控制器显示自定义转换,因此fromViewController将变暗,就像呈现AlertViewController一样。 我创建了自己的customTransition管理器:

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

    let fadeRect = CGRectMake(0, 0, fromView.frame.size.width, fromView.frame.size.height)
    let fadeView = UIView(frame: fadeRect)

    if  (self.presenting == true){

        fadeView.backgroundColor = UIColor.blackColor()
        fadeView.alpha = 0

        fromView.addSubview(fadeView)
        container.insertSubview(fadeView, aboveSubview: fromView)
    } else {

//             adding subviews to container
    }

    let duration = self.transitionDuration(transitionContext)

    UIView.animateWithDuration(duration, animations: { () -> Void in

        if  (self.presenting == true){
            fadeView.alpha = 0.5
        } else {

        }
        }) { (Bool) -> Void in
            if self.presenting{
                container.addSubview(toView)
            }

            transitionContext.completeTransition(true)
    }
}

}

我将过渡经理分配到toViewController,如下所示:

var purchaseSpecialItemsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("specialItemPurchaseVC") as! BRSpecialItemPurchaseViewController
    purchaseSpecialItemsViewController.transitioningDelegate = self.fadedTransitionManager

    self.presentViewController(purchaseSpecialItemsViewController, animated: true, completion: nil)

我要显示的视图控制器位于具有固定大小的故事板中。它有一个清晰的背景,我在那里添加了一个tableview,我想要在屏幕的中心。

TableView显示没有问题,但我根本看不到fromViewController。它只是黑色,即使我将黑色背景的alpha设置为0,后来将其设置为0.5。

我以模态方式呈现视图控制器,是否导致问题?我应该使用推送和弹出操作吗?

1 个答案:

答案 0 :(得分:0)

如下所述:http://mathewsanders.com/custom-menu-transitions-in-swift/

我需要像这样添加模态演示文稿样式:

purchaseSpecialItemsViewController.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen

而是获取ViewControllersViews,使用

let screens: (from: UIViewController, to: UIViewController) = (transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!, transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!)

访问ViewControllers。