2016-11-11 111 views
0

我想實現一個自定義的彈出式自定義轉換,但我的委託方法根本沒有被調用。這是我的轉變代表:UIViewControllerTransitioningDelegate方法不叫

public final class ModalTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate { 

    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { 
     let controller = ModalPresentationController(presentedViewController: presented, presenting: presenting) 

     return controller 
    } 

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

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

這是我的彈出視圖控制器:

class StopWorkoutViewController: UIViewController { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     commonInit() 
    } 

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { 
     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
     commonInit() 
    } 

    func commonInit() { 

     let transitioner = ModalTransitioningDelegate() 
     modalPresentationStyle = .custom 
     transitioningDelegate = transitioner 
    } 
} 

這就是我目前的彈出:

@IBAction func test(_ sender: Any) { 
    let popup = UIStoryboard(name: "Popups", bundle: nil).instantiateInitialViewController() as! StopWorkoutViewController 
    present(popup, animated: true, completion: nil) 
} 

而且這是在IB視圖控制器:

Popup ViewController

顯示彈出窗口,但全屏顯示。

回答

1

這是錯誤的

func commonInit() { 

     let transitioner = ModalTransitioningDelegate() 
     modalPresentationStyle = .custom 
     transitioningDelegate = transitioner 
    } 

因爲你要動畫的StopWorkoutViewController。你需要設置過渡委託作爲

@IBAction func test(_ sender: Any) { 
    let transitioner = ModalTransitioningDelegate() 
    let popup = UIStoryboard(name: "Popups", bundle: nil).instantiateInitialViewController() as! StopWorkoutViewController 
    popup.transitioningDelegate = transitioner 
    present(popup, animated: true, completion: nil) 
} 
1

過渡的問題是,transitioningDelegate是弱屬性,所以您分配的類將在轉換有機會運行之前獲得釋放。查看我的回答here,瞭解如何在調試器中捕獲此問題的示例。