2016-12-18 4 views
7

私はライブラリを開発しており、2つのView Controller間でデフォルトのカスタムトランジションを提供したいと考えています。ユーザーは自分の実装を提供することもできます。 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

これを行う最も洗練された(そしてプロトコル指向の)方法の1つは、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:)という異なる実装を簡単に提供することもできます。ユーザーは、上記の2つの機能の独自の実装をどこにでも追加することができます。また、関連するオブジェクトのような面倒な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の概念に対処することなく、欲しいものを提供します。がんばろう!

関連する問題