2016-12-18 126 views
7

我正在開發一個圖書館,我想提供兩個視圖控制器之間的默認定製化,用戶也可以提供自己的實現,它涉及到我的腦海裏的第一個想法是重寫UIViewController和實施UIViewControllerTransitioningDelegate然後用戶可以繼承我的CustomTransitionViewController是否是實現它的最佳方式?任何限制?有沒有更優雅的方式使用協議,例如默認實現?默認自定義轉換爲UIViewController中

import UIKit 

class CustomTransitionViewController: UIViewController, UIViewControllerTransitioningDelegate { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.transitioningDelegate = self 
    } 

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

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8) 
    } 

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8) 
    } 
} 

回答

0

你也可以只作出,做所有你需要的東西,還符合你的協議UIViewController擴展,使用戶即禁用默認過渡。

舉個例子:

protocol ThorsHammer { 
    var isDefaultTransitionEnabled: Bool { get set } 
} 

extension UIViewController: ThorsHammer { 
    var isDefaultTransitionEnabled: Bool { /* this part sucks */ } 

    // Check if the default transition should happen and 
    // do some transition magic 
} 

如何附加存儲性能擴展,你可以看看this

所有這一切,如果你能找到一種方法來設置transitioningDelegate不知何故在擴展,否則你會必須繼承

5

我覺得最優雅(和麪向協議)的方式來做到這將是一個UIViewControllerTransitioningDelegate擴展之一。擴展協議併爲animationController(forPresented: presenting: source:)animationController(forDismissed:)提供默認實現。擴展會是這個樣子:

extension UIViewControllerTransitioningDelegate where Self: UIViewController { 
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8) 
    } 

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8) 
    } 
} 

然後告訴你的用戶,以延長其視圖控制器符合本協議。既然你已經實現的協議擴展了唯一的要求,他們需要做的就是添加一致性聲明,就像這樣:

class MyViewController: UIViewController, UIViewControllerTransitioningDelegate { } 

如果你想擴展所有UIViewController s到這樣做,你可以做到這一點一個空的分機:

extension UIViewController: UIViewControllerTransitioningDelegate { } 

這應該有效。在我看來,這是一個非常乾淨,優雅的解決方案,不需要不必要的子類化。它也可以很容易地在每個視圖控制器提供的animationController(forPresented: presenting: source:)animationController(forDismissed:)不同的實現。您的用戶只需將他們自己的兩個上述功能的實現添加到他們想要的任何地方。此外,你不需要處理混亂的Objective C東西,像關聯的對象。

如果您在某種預先打包的軟件包中提供了此功能,並且您希望隱藏底層實現,則可以聲明自己的協議,使其成爲UIViewControllerTransitioningDelegate的子協議,並以類似方式對其進行擴展,並讓用戶遵守您的自定義協議,而不是UIViewControllerTransitioningDelegate

protocol MyProtocol: UIViewControllerTransitioningDelegate { 
    //Add your own requirements (if you have any). 
} 

extension MyProtocol where Self: UIViewController { 
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8) 
    } 

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8) 
    } 

    //Satisfy your requirements (or let your users do it). 
} 

class MyViewController: UIViewController, MyProtocol { } 

extension UIViewController: MyProtocol { } 

你採用何種方式,該解決方案爲您提供了你想要的東西,而不讓你對付外來的子類或醜陋的Objective C的概念。祝你好運!

相關問題