-
Notifications
You must be signed in to change notification settings - Fork 8
/
BackgroundPresentationController.swift
95 lines (73 loc) · 3.16 KB
/
BackgroundPresentationController.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
//
// BackgroundPresentationController.swift
// RPModalGestureTransition
//
// Created by naoyashiga on 2015/09/28.
// Copyright © 2015年 naoyashiga. All rights reserved.
//
import UIKit
struct ModalSizeRatio {
static let width: CGFloat = 0.85
static let height: CGFloat = 0.8
}
class BackgroundPresentationController: UIPresentationController {
var dimmingView: UIView!
override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
setupDimmingView()
}
func setupDimmingView() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: "dimmingViewTapped:")
dimmingView = UIView()
dimmingView.backgroundColor = UIColor.blackColor()
dimmingView.addGestureRecognizer(tapRecognizer)
}
func dimmingViewTapped(tapRecognizer: UITapGestureRecognizer) {
presentingViewController.dismissViewControllerAnimated(true, completion: nil)
}
override func containerViewWillLayoutSubviews() {
guard let containerView = containerView else {
return
}
guard let presentedView = presentedView() else {
return
}
dimmingView.frame = containerView.bounds
presentedView.frame = frameOfPresentedViewInContainerView()
presentedView.layer.borderWidth = 0.0
}
override func frameOfPresentedViewInContainerView() -> CGRect {
guard let containerView = containerView else {
return CGRectZero
}
var presentedViewFrame = CGRectZero
let width:CGFloat = containerView.bounds.size.width * ModalSizeRatio.width
let height:CGFloat = containerView.bounds.size.height * ModalSizeRatio.height
presentedViewFrame.size = CGSizeMake(width,height)
presentedViewFrame.origin = CGPointMake(containerView.bounds.size.width / 2.0, containerView.bounds.size.height / 2.0)
presentedViewFrame.origin.x -= presentedViewFrame.size.width / 2.0;
presentedViewFrame.origin.y -= presentedViewFrame.size.height / 2.0;
return presentedViewFrame
}
override func presentationTransitionWillBegin() {
guard let containerView = containerView else {
return
}
dimmingView.frame = containerView.bounds
dimmingView.alpha = 0.0
containerView.insertSubview(dimmingView, atIndex: 0)
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({ (context) -> Void in
self.dimmingView.alpha = 0.8
}, completion: nil)
}
override func dismissalTransitionWillBegin() {
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({ (context) -> Void in
self.dimmingView.alpha = 0.0
}, completion: nil)
}
override func dismissalTransitionDidEnd(completed: Bool) {
if completed {
dimmingView.removeFromSuperview()
}
}
}