-
Notifications
You must be signed in to change notification settings - Fork 5
/
UDatePicker.swift
138 lines (108 loc) · 4.5 KB
/
UDatePicker.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// DatePickerView.swift
//
// Created by 4074 on 16/6/12.
// Copyright © 2016年 4074. All rights reserved.
//
import UIKit
open class UDatePicker: UIViewController {
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open var picker: UDatePickerView!
public init(frame: CGRect, willDisappear: ((Date?) -> Void)? = nil, didDisappear: ((Date?) -> Void)? = nil) {
super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
// init picker view
let view = UDatePickerView(frame: frame)
view.completion = { date in
if willDisappear != nil {
willDisappear!(date)
}
self.dismiss(animated: true) {
if didDisappear != nil {
didDisappear!(date)
}
}
}
picker = view
self.view = view
}
// present the view controller
open func present(_ previous: UIViewController) {
previous.present(self, animated: true, completion: nil)
}
open class UDatePickerView: UIView {
fileprivate var completion: ((Date?) -> Void)?
// current date be shown
open var date = Date() {
didSet {
datePicker.setDate(date, animated: false)
}
}
open var duration = 0.4
// height of views
open var height = (
widget: CGFloat(248),
picker: CGFloat(216),
bar: CGFloat(32)
)
public let widgetView = UIView()
public let blankView = UIView()
public let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .extraLight))
public let datePicker = UIDatePicker()
public let barView = UIView()
public let doneButton = UIButton()
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override open func layoutSubviews() {
let frame = self.frame
// reset frame of views
widgetView.frame = CGRect(x: 0, y: frame.height - height.widget, width: frame.width, height: height.widget)
blankView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height - height.widget)
datePicker.frame = CGRect(x: 0, y: height.bar, width: frame.width, height: height.picker)
barView.frame = CGRect(x: 0, y: 0, width: frame.width, height: height.bar)
blurView.frame = barView.frame
// set button flexible width, with 16 padding
doneButton.sizeToFit()
let width = doneButton.frame.size.width + 16
doneButton.frame = CGRect(x: frame.width - width, y: 0, width: width, height: height.bar)
}
fileprivate func initView() {
self.addSubview(widgetView)
// blur view
widgetView.addSubview(blurView)
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// blank view
self.addSubview(blankView)
let tapBlankGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleBlankView))
blankView.addGestureRecognizer(tapBlankGesture)
// date picker view
widgetView.addSubview(datePicker)
datePicker.datePickerMode = .date
datePicker.backgroundColor = UIColor.white
// bar view and done button
widgetView.addSubview(barView)
barView.addSubview(doneButton)
doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
doneButton.setTitle("Done", for: UIControl.State())
doneButton.setTitleColor(self.tintColor, for: UIControl.State())
doneButton.addTarget(self, action: #selector(self.handleDoneButton), for: .touchUpInside)
}
@objc func handleBlankView() {
if completion != nil {
completion!(nil)
}
}
@objc func handleDoneButton() {
if completion != nil {
completion!(datePicker.date)
}
}
}
}