Bunu yapmanın en zarif (ve protokol yönelimli) yollarından biri, UIViewControllerTransitioningDelegate
uzantısıyla olurdu. Protokolü uzatın ve animationController(forPresented: presenting: source:)
ve animationController(forDismissed:)
için varsayılan bir uygulama sağlayın.
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)
}
}
Ve sonra bu protokole uymak için kendi bakış denetleyicileri genişletmek için kullanıcılarınızı: uzatma böyle bir şey olmazdı. Eğer bunu yapabilir,
class MyViewController: UIViewController, UIViewControllerTransitioningDelegate { }
tüm UIViewController
ler bunu uzatmak istiyorsanız: Zaten bir protokol uzantısı sadece gereksinimlerini hayata beri yapmaları gereken tüm şöyle, uygunluk beyanı ekleyin Boş bir uzantı:
extension UIViewController: UIViewControllerTransitioningDelegate { }
Çalışması gerekir. Benim düşünceme göre, gereksiz alt sınıflama gerektirmeyen çok temiz ve zarif bir çözüm. Ayrıca, her görünüm denetleyicisine farklı uygulamaların animationController(forPresented: presenting: source:)
ve animationController(forDismissed:)
olmasını kolaylaştırır. Kullanıcılarınız, istedikleri yerde, yukarıda bahsedilen iki işlevden kendi uygulamalarını ekleyebilirler. Ayrıca, ilişkili nesneler gibi dağınık Objektif C şeyleri ile uğraşmak zorunda değilsiniz.
Bu işlevselliği bir çeşit hazır ambalajlı paket halinde sunuyorsanız ve temel uygulamayı gizlemek istiyorsanız, kendi protokolünüzü bildirebilir, UIViewControllerTransitioningDelegate
numaralı alt kümeyi oluşturabilir, benzer şekilde genişletebilir ve kullanıcılarınızın uygun olmasını sağlayabilirsiniz. yerine UIViewControllerTransitioningDelegate
bilgiler özel protokol:
çektiğiniz Hangi yol
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 { }
, bu çözüm size yabancı altsınıflamayla ya da çirkin Objective C kavramları ile uğraşmak yapmadan istediğini sen verir. İyi şanslar!