/
CustomPresent.swift
86 lines (69 loc) · 2.96 KB
/
CustomPresent.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
//
// CustomPresent.swift
// 25分
//
// Created by blues on 15/12/16.
// Copyright © 2015年 blues. All rights reserved.
//
import Foundation
import UIKit
class CustomPresent: UIPresentationController {
var dimingView:UIView!
var top = true
override func frameOfPresentedViewInContainerView() -> CGRect {
if top{
return CGRectMake(0, 0, CGRectGetWidth(containerView!.bounds), 1 * CGRectGetHeight(containerView!.bounds) / 4)
}
return CGRectMake(0, 2 * CGRectGetHeight(containerView!.bounds) / 3, CGRectGetWidth(containerView!.bounds), 1 * CGRectGetHeight(containerView!.bounds) / 3)
}
override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
self.dimingView = UIView()
self.dimingView.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
self.dimingView.alpha = 0
}
func dimingViewTap(){
presentedViewController.modalPresentationStyle = .Custom
presentedViewController.transitioningDelegate = transitionDelegate
presentedViewController.dismissViewControllerAnimated(true, completion: nil)
}
override func presentationTransitionWillBegin() {
//add gesture
let touchUpGecture = UITapGestureRecognizer(target: self, action: "dimingViewTap")
var swipeGesture:UISwipeGestureRecognizer!
if top{
swipeGesture = UISwipeGestureRecognizer(target: self, action: "dimingViewTap")
swipeGesture.direction = UISwipeGestureRecognizerDirection.Up
}else{
swipeGesture = UISwipeGestureRecognizer(target: self, action: "dimingViewTap")
swipeGesture.direction = UISwipeGestureRecognizerDirection.Down
}
self.dimingView.addGestureRecognizer(touchUpGecture)
self.dimingView.addGestureRecognizer(swipeGesture)
//transition
let containerView = self.containerView!
self.dimingView.frame = containerView.bounds
self.dimingView.alpha = 0
containerView.insertSubview(self.dimingView, atIndex: 0)
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
i in
self.dimingView.alpha = 1.0
}, completion: nil)
}
override func presentationTransitionDidEnd(completed: Bool) {
if(!completed){
self.dimingView.removeFromSuperview()
}
}
override func dismissalTransitionWillBegin() {
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
i in
self.dimingView.alpha = 0.0
}, completion: nil)
}
override func dismissalTransitionDidEnd(completed: Bool) {
if(completed){
self.dimingView.removeFromSuperview()
}
}
}