|
| 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 Transitioning |
| 19 | + |
| 20 | +// This example demonstrates the minimal path to building a custom transition using the Material |
| 21 | +// Motion Transitioning APIs in Swift. The essential steps have been documented below. |
| 22 | + |
| 23 | +class MenuInteractiveExampleViewController: ExampleViewController { |
| 24 | + |
| 25 | + func didTap() { |
| 26 | + let modalViewController = ModalInteractiveViewController() |
| 27 | + |
| 28 | + // The transition controller is an associated object on all UIViewController instances that |
| 29 | + // allows you to customize the way the view controller is presented. The primary API on the |
| 30 | + // controller that you'll make use of is the `transition` property. Setting this property will |
| 31 | + // dictate how the view controller is presented. For this example we've built a custom |
| 32 | + // FadeTransition, so we'll make use of that now: |
| 33 | + modalViewController.transitionController.transition = MenuTransition() |
| 34 | + modalViewController.transitionController.interactiveTransition = MenuInteractiveTransition() |
| 35 | + |
| 36 | + // Note that once we assign the transition object to the view controller, the transition will |
| 37 | + // govern all subsequent presentations and dismissals of that view controller instance. If we |
| 38 | + // want to use a different transition (e.g. to use an edge-swipe-to-dismiss transition) then we |
| 39 | + // can simply change the transition object before initiating the transition. |
| 40 | + |
| 41 | + present(modalViewController, animated: true) |
| 42 | + } |
| 43 | + |
| 44 | + override func viewDidLoad() { |
| 45 | + super.viewDidLoad() |
| 46 | + |
| 47 | + let label = UILabel(frame: view.bounds) |
| 48 | + label.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 49 | + label.textColor = .white |
| 50 | + label.textAlignment = .center |
| 51 | + label.text = "Swipe from left edge to start the transition" |
| 52 | + view.addSubview(label) |
| 53 | + |
| 54 | + let tap = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(edgePanGesture)) |
| 55 | + tap.edges = .left |
| 56 | + view.addGestureRecognizer(tap) |
| 57 | + |
| 58 | + navigationController?.interactivePopGestureRecognizer?.isEnabled = false |
| 59 | + } |
| 60 | + |
| 61 | + override func exampleInformation() -> ExampleInfo { |
| 62 | + return .init(title: type(of: self).catalogBreadcrumbs().last!, |
| 63 | + instructions: "Tap to present a modal transition.") |
| 64 | + } |
| 65 | + |
| 66 | + var percentage = CGFloat(0.01) |
| 67 | + func edgePanGesture(_ sender: UIScreenEdgePanGestureRecognizer) { |
| 68 | + let translation = sender.location(in: sender.view?.superview) |
| 69 | + switch sender.state { |
| 70 | + case .began: |
| 71 | + didTap() |
| 72 | + case .changed: |
| 73 | + percentage = translation.x / ((sender.view!.frame.width)/2) |
| 74 | + percentage = min(percentage, 0.99) |
| 75 | + interactiveTransitionContext?.updatePercent(percentage) |
| 76 | + case .ended: |
| 77 | + if percentage > 0.8 { |
| 78 | + interactiveTransitionContext?.finishInteractiveTransition() |
| 79 | + } else { |
| 80 | + interactiveTransitionContext?.cancelInteractiveTransition() |
| 81 | + } |
| 82 | + interactiveTransitionContext = nil |
| 83 | + percentage = CGFloat(0.01) |
| 84 | + default: |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Transitions must be NSObject types that conform to the Transition protocol. |
| 91 | +private final class MenuTransition: NSObject, Transition { |
| 92 | + |
| 93 | + // The sole method we're expected to implement, start is invoked each time the view controller is |
| 94 | + // presented or dismissed. |
| 95 | + func start(with context: TransitionContext) { |
| 96 | + let fromVC = context.backViewController |
| 97 | + let toVC = context.foreViewController |
| 98 | + let containerView = context.containerView |
| 99 | + |
| 100 | + if(context.direction == .forward) { |
| 101 | + containerView.insertSubview(toVC.view, aboveSubview: fromVC.view) |
| 102 | + toVC.view.frame.origin.x = -1 * toVC.view.frame.width |
| 103 | + UIView.animate( |
| 104 | + withDuration: context.duration, |
| 105 | + delay: 0, |
| 106 | + options: .curveLinear, |
| 107 | + animations: { |
| 108 | + toVC.view.frame.origin.x = -1 * (toVC.view.frame.width / 2) |
| 109 | + }, |
| 110 | + completion: { _ in |
| 111 | + let deadlineTime = DispatchTime.now() + .milliseconds(100) |
| 112 | + DispatchQueue.main.asyncAfter(deadline: deadlineTime) { |
| 113 | + context.transitionDidEnd() |
| 114 | + } |
| 115 | + } |
| 116 | + ) |
| 117 | + if let snapshot = fromVC.view.snapshotView(afterScreenUpdates: false) { |
| 118 | + snapshot.isUserInteractionEnabled = false |
| 119 | + containerView.insertSubview(snapshot, belowSubview: toVC.view) |
| 120 | + snapshot.tag = 2000 |
| 121 | + } |
| 122 | + } else { |
| 123 | + UIView.animate( |
| 124 | + withDuration: context.duration, |
| 125 | + delay: 0, |
| 126 | + options: .curveLinear, |
| 127 | + animations: { |
| 128 | + toVC.view.frame.origin.x = -1 * toVC.view.frame.width |
| 129 | + }, |
| 130 | + completion: { _ in |
| 131 | + |
| 132 | + let deadlineTime = DispatchTime.now() + .milliseconds(100) |
| 133 | + DispatchQueue.main.asyncAfter(deadline: deadlineTime) { |
| 134 | + context.transitionDidEnd() |
| 135 | + } |
| 136 | + |
| 137 | + if(context.wasCancelled == false) { |
| 138 | + containerView.viewWithTag(2000)?.removeFromSuperview() |
| 139 | + containerView.insertSubview(toVC.view, aboveSubview: fromVC.view) |
| 140 | + } |
| 141 | + } |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +final class MenuInteractiveTransition: NSObject, InteractiveTransition { |
| 148 | + func isInteractive(_ context: TransitionContext) -> Bool { |
| 149 | + return true |
| 150 | + } |
| 151 | + |
| 152 | + func start(withInteractiveContext context: InteractiveTransitionContext) { |
| 153 | + context.transitionContext.sourceViewController!.interactiveTransitionContext = context |
| 154 | + context.transitionContext.foreViewController.interactiveTransitionContext = context |
| 155 | + } |
| 156 | +} |
0 commit comments