Skip to content

Commit 3b2cb4c

Browse files
committed
Finished UIPreviewInteraction example, I think
1 parent a0f8440 commit 3b2cb4c

File tree

1 file changed

+7
-81
lines changed

1 file changed

+7
-81
lines changed

bk2ch06p300customAnimation3b/ch19p620customAnimation1/AppDelegate.swift

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,12 @@ class AppDelegate : UIResponder, UIApplicationDelegate, UITabBarControllerDelega
1212

1313
var prev : UIPreviewInteraction! // *
1414

15-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
15+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
1616

1717
let tbc = self.window!.rootViewController as! UITabBarController
1818
tbc.delegate = self
1919

20-
let sep = UIScreenEdgePanGestureRecognizer(target:self, action:#selector(pan))
21-
sep.edges = UIRectEdge.right
22-
tbc.view.addGestureRecognizer(sep)
23-
sep.delegate = self
24-
25-
let sep2 = UIScreenEdgePanGestureRecognizer(target:self, action:#selector(pan))
26-
sep2.edges = UIRectEdge.left
27-
tbc.view.addGestureRecognizer(sep2)
28-
sep2.delegate = self
29-
30-
let prev = UIPreviewInteraction(view: tbc.view)
20+
let prev = UIPreviewInteraction(view: tbc.tabBar)
3121
prev.delegate = self
3222
self.prev = prev
3323

@@ -49,10 +39,7 @@ class AppDelegate : UIResponder, UIApplicationDelegate, UITabBarControllerDelega
4939
extension AppDelegate : UIPreviewInteractionDelegate {
5040
func previewInteractionShouldBegin(_ previewInteraction: UIPreviewInteraction) -> Bool {
5141
let tbc = self.window!.rootViewController as! UITabBarController
52-
let loc = previewInteraction.location(in:tbc.view)
53-
if tbc.tabBar.frame.contains(loc) {
54-
return false // don't conflict with buttons
55-
}
42+
let loc = previewInteraction.location(in:tbc.tabBar)
5643
if loc.x > tbc.view!.bounds.midX {
5744
if tbc.selectedIndex < tbc.viewControllers!.count - 1 {
5845
self.interacting = true
@@ -107,67 +94,6 @@ extension AppDelegate : UIPreviewInteractionDelegate {
10794

10895
}
10996

110-
extension AppDelegate : UIGestureRecognizerDelegate {
111-
112-
func gestureRecognizerShouldBegin(_ g: UIGestureRecognizer) -> Bool {
113-
return false // turn this off entirely
114-
let tbc = self.window!.rootViewController as! UITabBarController
115-
var result = false
116-
117-
if (g as! UIScreenEdgePanGestureRecognizer).edges == .right {
118-
result = (tbc.selectedIndex < tbc.viewControllers!.count - 1)
119-
}
120-
else {
121-
result = (tbc.selectedIndex > 0)
122-
}
123-
return result
124-
}
125-
126-
func pan(_ g:UIScreenEdgePanGestureRecognizer) {
127-
128-
switch g.state {
129-
case .began:
130-
self.interacting = true
131-
let tbc = self.window!.rootViewController as! UITabBarController
132-
if g.edges == .right {
133-
tbc.selectedIndex = tbc.selectedIndex + 1
134-
} else {
135-
tbc.selectedIndex = tbc.selectedIndex - 1
136-
}
137-
case .changed:
138-
let v = g.view!
139-
let delta = g.translation(in:v)
140-
let percent = abs(delta.x/v.bounds.size.width)
141-
self.anim?.fractionComplete = percent
142-
self.context?.updateInteractiveTransition(percent)
143-
case .ended:
144-
145-
// this is the money shot!
146-
// with a property animator, the whole notion of "hurry home" is easy -
147-
// including "hurry back to start"
148-
149-
let anim = self.anim as! UIViewPropertyAnimator
150-
anim.pauseAnimation()
151-
152-
if anim.fractionComplete < 0.5 {
153-
anim.isReversed = true
154-
}
155-
anim.continueAnimation(
156-
withTimingParameters:
157-
UICubicTimingParameters(animationCurve:.linear),
158-
durationFactor: 0.2)
159-
160-
case .cancelled:
161-
162-
self.anim?.pauseAnimation()
163-
self.anim?.stopAnimation(false)
164-
self.anim?.finishAnimation(at: .start)
165-
166-
default: break
167-
}
168-
}
169-
}
170-
17197
extension AppDelegate : UIViewControllerInteractiveTransitioning {
17298

17399
// called if we are interactive
@@ -196,17 +122,17 @@ extension AppDelegate : UIViewControllerAnimatedTransitioning {
196122
return self.anim!
197123
}
198124

199-
let vc1 = ctx.viewController(forKey:UITransitionContextFromViewControllerKey)!
200-
let vc2 = ctx.viewController(forKey:UITransitionContextToViewControllerKey)!
125+
let vc1 = ctx.viewController(forKey:.from)!
126+
let vc2 = ctx.viewController(forKey:.to)!
201127

202128
let con = ctx.containerView
203129

204130
let r1start = ctx.initialFrame(for:vc1)
205131
let r2end = ctx.finalFrame(for:vc2)
206132

207133
// new in iOS 8, use these instead of assuming that the views are the views of the vcs
208-
let v1 = ctx.view(forKey:UITransitionContextFromViewKey)!
209-
let v2 = ctx.view(forKey:UITransitionContextToViewKey)!
134+
let v1 = ctx.view(forKey:.from)!
135+
let v2 = ctx.view(forKey:.to)!
210136

211137
// which way we are going depends on which vc is which
212138
// the most general way to express this is in terms of index number

0 commit comments

Comments
 (0)