私はナビゲーションコントローラを持っており、そのナビゲーションコントローラの中に私はホームスクリーンを持っています。ホーム画面から別の画面に行くボタンをクリックします。カスタムトランジション
しかし、ナビゲーションコントローラを使用する際の標準的なショーアニメーションは、サイドからスライドすることですが、私がしたいことは、ビューコントローラが画面の下からスライドして、到達すると一種のバウンスアニメーションを作成することですトップ。
私はナビゲーションコントローラを持っており、そのナビゲーションコントローラの中に私はホームスクリーンを持っています。ホーム画面から別の画面に行くボタンをクリックします。カスタムトランジション
しかし、ナビゲーションコントローラを使用する際の標準的なショーアニメーションは、サイドからスライドすることですが、私がしたいことは、ビューコントローラが画面の下からスライドして、到達すると一種のバウンスアニメーションを作成することですトップ。
UIViewControllerAnimatedTransitioning
とUIViewControllerTransitioningDelegate
プロトコルを覚えておくために、カスタムトランジションを使用したいと思った人は誰でも。さて、上記の方法は、アニメーションの世話をするNSObject
import UIKit
class CustomPushAnimation: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.2
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerVw = transitionContext.containerView
let fromViewController = transitionContext.viewController(forKey: .from)
let toViewController = transitionContext.viewController(forKey: .to)
guard let fromVc = fromViewController, let toVc = toViewController else { return }
let finalFrame = transitionContext.finalFrame(for: toVc)
//For different animation you can play around this line by changing frame
toVc.view.frame = finalFrame.offsetBy(dx: 0, dy: finalFrame.size.height/2)
containerVw.addSubview(toVc.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
toVc.view.frame = finalFrame
fromVc.view.alpha = 0.5
}, completion: {(finished) in
transitionContext.completeTransition(finished)
fromVc.view.alpha = 1.0
})
}
}
から継承あなたのcustomclass内
UIViewControllerAnimatedTransitioning
に準拠しています。その後 上記のオブジェクトを作成し、yourViewControllerクラス内
import UIKit
class YourViewController: UIViewController {
lazy var customPushAnimation: CustomPushAnimation = {
return CustomPushAnimation()
}()
func openViewControler() {
}let vc =//Assuming your view controller which you want to open
let navigationController = UINavigationController(rootViewController: vc)
//Set transitioningDelegate to invoke protocol method
navigationController.transitioningDelegate = self
present(navigationController, animated: true, completion: nil)
}
注意を使用します。アニメーションを見るために。 ViewControllerを表示している間はアニメーションフラグをfalseに設定しないでください。 そうしないと、アニメーションは になります。
あなたは、プロトコル方法上記のViewControllerが呼び出さ うとアニメーションの魔法が表示されます提示するたびに最後にYourViewcontroller
クラス
extension YourViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPushAnimation
}
内部
UIViewControllerTransitioningDelegate
プロトコルメソッドを実装します。
これまでに何を試みましたか?使用しようとしているコードを表示し、どこに問題があるのか説明してください。何も試していないのであれば、Google(またはお気に入りの検索エンジン)にジャンプして、 'uinavigationcontroller custom transition'を検索してください...たくさんの例、ディスカッション、チュートリアルなどを見つけることができます。 – DonMag