|
| 1 | +/* |
| 2 | + Copyright 2017-present The Material Motion Authors. All Rights Reserved. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import UIKit |
| 18 | +import MotionTransitioning |
| 19 | + |
| 20 | +// This example demonstrates how to build a custom UINavigationController transition using the |
| 21 | +// Motion Transitioning APIs in Swift. The essential steps have been documented below. |
| 22 | + |
| 23 | +class NavControllerFadeExampleViewController: ExampleViewController { |
| 24 | + |
| 25 | + func didTap() { |
| 26 | + let modalViewController = ModalViewController() |
| 27 | + modalViewController.title = "Example view controller" |
| 28 | + |
| 29 | + // The transition controller is an associated object on all UIViewController instances that |
| 30 | + // allows you to customize the way the view controller is presented. The primary API on the |
| 31 | + // controller that you'll make use of is the `transition` property. Setting this property will |
| 32 | + // dictate how the view controller is presented. For this example we've built a custom |
| 33 | + // FadeTransition, so we'll make use of that now: |
| 34 | + modalViewController.transitionController.transition = FadeTransition() |
| 35 | + |
| 36 | + cachedNavDelegate = navigationController?.delegate |
| 37 | + |
| 38 | + // In order to customize navigation controller transitions you must implement the necessary |
| 39 | + // delegate methods. By setting the shared transition navigation controller delegate instance |
| 40 | + // we're able to customize push/pop transitions using our transitionController. |
| 41 | + |
| 42 | + navigationController?.delegate = TransitionNavigationControllerDelegate.sharedDelegate() |
| 43 | + |
| 44 | + navigationController?.pushViewController(modalViewController, animated: true) |
| 45 | + } |
| 46 | + private var cachedNavDelegate: UINavigationControllerDelegate? |
| 47 | + |
| 48 | + override func viewDidDisappear(_ animated: Bool) { |
| 49 | + super.viewDidDisappear(animated) |
| 50 | + |
| 51 | + if parent == nil { // Popped off |
| 52 | + // Restore the previous delegate, if any. |
| 53 | + navigationController?.delegate = cachedNavDelegate |
| 54 | + |
| 55 | + cachedNavDelegate = nil |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + override func viewDidLoad() { |
| 60 | + super.viewDidLoad() |
| 61 | + |
| 62 | + let label = UILabel(frame: view.bounds) |
| 63 | + label.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 64 | + label.textColor = .white |
| 65 | + label.textAlignment = .center |
| 66 | + label.text = "Tap to start the transition" |
| 67 | + view.addSubview(label) |
| 68 | + |
| 69 | + let tap = UITapGestureRecognizer(target: self, action: #selector(didTap)) |
| 70 | + view.addGestureRecognizer(tap) |
| 71 | + } |
| 72 | + |
| 73 | + override func exampleInformation() -> ExampleInfo { |
| 74 | + return .init(title: type(of: self).catalogBreadcrumbs().last!, |
| 75 | + instructions: "Tap to present a modal transition.") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Transitions must be NSObject types that conform to the Transition protocol. |
| 80 | +private final class FadeTransition: NSObject, Transition { |
| 81 | + |
| 82 | + // The sole method we're expected to implement, start is invoked each time the view controller is |
| 83 | + // presented or dismissed. |
| 84 | + func start(with context: TransitionContext) { |
| 85 | + CATransaction.begin() |
| 86 | + |
| 87 | + CATransaction.setCompletionBlock { |
| 88 | + // Let UIKit know that the transition has come to an end. |
| 89 | + context.transitionDidEnd() |
| 90 | + } |
| 91 | + |
| 92 | + let fade = CABasicAnimation(keyPath: "opacity") |
| 93 | + |
| 94 | + fade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) |
| 95 | + |
| 96 | + // Define our animation assuming that we're going forward (presenting)... |
| 97 | + fade.fromValue = 0 |
| 98 | + fade.toValue = 1 |
| 99 | + |
| 100 | + // ...and reverse it if we're going backwards (dismissing). |
| 101 | + if context.direction == .backward { |
| 102 | + let swap = fade.fromValue |
| 103 | + fade.fromValue = fade.toValue |
| 104 | + fade.toValue = swap |
| 105 | + } |
| 106 | + |
| 107 | + // Add the animation... |
| 108 | + context.foreViewController.view.layer.add(fade, forKey: fade.keyPath) |
| 109 | + |
| 110 | + // ...and ensure that our model layer reflects the final value. |
| 111 | + context.foreViewController.view.layer.setValue(fade.toValue, forKeyPath: fade.keyPath!) |
| 112 | + |
| 113 | + CATransaction.commit() |
| 114 | + } |
| 115 | +} |
0 commit comments