This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathInteractivePushBackTransitionExample.swift
103 lines (76 loc) · 3.45 KB
/
InteractivePushBackTransitionExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Copyright 2016-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
import MaterialMotion
class InteractivePushBackTransitionExampleViewController: ExampleViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTap)))
}
func didTap() {
let vc = ModalViewController()
present(vc, animated: true)
}
override func exampleInformation() -> ExampleInfo {
return .init(title: type(of: self).catalogBreadcrumbs().last!,
instructions: "Tap to present a modal transition.")
}
}
private class ModalViewController: UIViewController, UIGestureRecognizerDelegate {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
transitionController.transition = PushBackTransition()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .primaryColor
scrollView = UIScrollView(frame: view.bounds)
scrollView.contentSize = .init(width: view.bounds.width, height: view.bounds.height * 10)
view.addSubview(scrollView)
let pan = UIPanGestureRecognizer()
pan.delegate = transitionController.topEdgeDismisserDelegate(for: scrollView)
transitionController.dismissWhenGestureRecognizerBegins(pan)
scrollView.panGestureRecognizer.require(toFail: pan)
view.addGestureRecognizer(pan)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
private class PushBackTransition: Transition {
func willBeginTransition(withContext ctx: TransitionContext, runtime: MotionRuntime) -> [Stateful] {
let draggable = Draggable(withFirstGestureIn: ctx.gestureRecognizers)
runtime.add(ChangeDirection(withVelocityOf: draggable.nextGestureRecognizer, whenNegative: .forward),
to: ctx.direction)
let bounds = ctx.containerView().bounds
let backPosition = CGPoint(x: bounds.midX, y: bounds.maxY + ctx.fore.view.bounds.height / 2)
let forePosition = CGPoint(x: bounds.midX, y: bounds.midY)
let movement = TransitionSpring(back: backPosition,
fore: forePosition,
direction: ctx.direction)
let scale = runtime.get(ctx.back.view.layer).scale
let tossable = Tossable(spring: movement, draggable: draggable)
runtime.connect(runtime.get(ctx.fore.view.layer).position.y()
.rewriteRange(start: movement.backwardDestination.y,
end: movement.forwardDestination.y,
destinationStart: 1,
destinationEnd: 0.95),
to: scale)
runtime.add(tossable, to: ctx.fore.view) { $0.xLocked(to: bounds.midX) }
return [tossable]
}
}