-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathJGTransitionExpandContract.swift
69 lines (54 loc) · 2.54 KB
/
JGTransitionExpandContract.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
//
// JGTransitionExpandContract.swift
// JGTransitions
//
// Created by Jeff on 1/9/15.
// Copyright (c) 2015 Jeff Greenberg. All rights reserved.
//
import UIKit
class JGTransitionExpandContract: NSObject, UIViewControllerAnimatedTransitioning {
var isPresenting = true
var focalPoint: CGPoint? // set to vanishing point or defaults to middle of screen
var duration = 1.75
var dampingRatio: CGFloat = 0.6
var transformScale: CGFloat = 0.14
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
if self.isPresenting {
toView.center = (focalPoint == nil) ? toView.center : focalPoint!
containerView!.addSubview(toView)
} else {
fromView.center = toView.center
containerView!.addSubview(toView)
containerView!.addSubview(fromView)
}
let presentingTransform = CGAffineTransformIdentity
let dismissingTransform = CGAffineTransformConcat(CGAffineTransformMakeScale(transformScale, transformScale), CGAffineTransformMakeRotation(CGFloat(8 * M_PI)))
let animatingView = self.isPresenting ? toView : fromView
animatingView.transform = isPresenting ? dismissingTransform : presentingTransform
UIView.animateWithDuration( self.transitionDuration(transitionContext),
delay: 0,
usingSpringWithDamping: dampingRatio,
initialSpringVelocity: 0,
options: [],
animations: {
if self.isPresenting {
toView.center = fromView.center
} else {
fromView.center = (self.focalPoint == nil) ? fromView.center : self.focalPoint!
}
animatingView.transform = self.isPresenting ? presentingTransform : dismissingTransform
},
completion: { finished in
containerView!.addSubview(toView)
fromView.removeFromSuperview()
transitionContext.completeTransition(true)
}
)
}
internal func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return duration
}
}