2017-07-28 24 views
0

SO(および他の場所)で多くの関連トピックが見つかりましたが、私の問題の解決策はまだ見つかりませんでした。 UIViewControllerTransitioningDelegateを使用して私のビューにカスタムアラートを表示したい。だから、最初に、私の最初のビューコントローラでは、ここでの呼び出しは、次のとおりです。UIViewControllerAnimatedTransitioning:カスタムビューで画面が黒くなっています

@IBAction func tappedButton(_ sender: Any) { 
    MyAlertViewController.presentIn(viewController: self) 
} 

そしてここでMyAlertViewControllerのコード:

import UIKit 

open class MyAlertViewController: UIViewController, UIViewControllerTransitioningDelegate { 

    @IBOutlet weak var overlayView: UIView? 
    @IBOutlet weak var alertView: UIView? 
    @IBOutlet weak var alertCenterYConstraint: NSLayoutConstraint? 

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { 
     super.init(nibName: "MyAlertView", bundle: nil) 
     self.transitioningDelegate = self 
    } 

    required public init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    static func presentIn(viewController: UIViewController) { 
     let alertViewController = MyAlertViewController() 

     if Thread.isMainThread { 
      viewController.present(alertViewController, animated: true, completion: nil) 
     } else { 
      DispatchQueue.main.async { 
       viewController.present(alertViewController, animated: true, completion: nil) 
      } 
     } 
    } 

    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return MyDismissAlertViewAnimationController() 
    } 

    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return MyPresentAlertViewAnimationController() 
    } 
} 

class MyPresentAlertViewAnimationController: NSObject, UIViewControllerAnimatedTransitioning { 
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return 0.3 
    } 

    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
     let toViewController: MyAlertViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as! MyAlertViewController 
     let duration = self.transitionDuration(using: transitionContext) 

     let containerView = transitionContext.containerView 
     toViewController.view.frame = containerView.frame 
     containerView.addSubview(toViewController.view) 

     toViewController.overlayView?.alpha = 0.0 
     UIView.animate(withDuration: duration, animations: { 
      toViewController.overlayView?.alpha = 0.6 
     }) 

     let finishFrame = toViewController.alertView?.frame 
     var startingFrame = finishFrame 
     startingFrame?.origin.y = -((finishFrame?.height)!) 
     toViewController.alertView?.frame = startingFrame! 

     UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: .layoutSubviews, animations: { 
      toViewController.alertView?.frame = finishFrame! 
     }, completion: { result in 
      transitionContext.completeTransition(result) 
     }) 
    } 
} 

class MyDismissAlertViewAnimationController: NSObject, UIViewControllerAnimatedTransitioning { 
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return 0.3 
    } 

    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
     let fromViewController: MyAlertViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as! MyAlertViewController 
     let duration = self.transitionDuration(using: transitionContext) 

     UIView.animate(withDuration: duration, animations: { 
      fromViewController.overlayView?.alpha = 0.0 
     }) 

     var finishFrame = fromViewController.alertView?.frame 
     finishFrame?.origin.y = -(finishFrame?.height)! 
     finishFrame?.origin.y = fromViewController.isDismissingByBottom ? fromViewController.view.frame.size.height : -(finishFrame?.height)! 

     UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .layoutSubviews, animations: { 
      fromViewController.alertView?.frame = finishFrame! 
     }, completion: { result in 
      transitionContext.completeTransition(true) 
     }) 
    } 
} 

アニメーションが正常に動作し、黒い画面しかcompleteTransition()への呼び出し後に表示されますあなたは以下を参照することができますよう:あなたの助けを

enter image description here

おかげで...

答えて

1

私はあなたがそうのようなあなたの背景色を設定するには、いずれかの必要があると思う:これはあなたが見る背景があなたのモーダルのちょうど背景色ではないことを確認します

self.view.backgroundColor = .clear 

それともoverCurrentContext

self.modalPresentationStyle = .overCurrentContext 
+0

どうもありがとうモーダルプレゼンテーションのスタイルを作ることによって、画面から削除する提示ビューコントローラを防ぎます! 'presentIn()'関数に 'alertViewController.modalPresentationStyle = .overCurrentContext'を追加することで問題が解決されました。 – Rob

関連する問題