Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default date & background tap #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Sources/DatePickerDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ open class DatePickerDialog: UIView {
private var datePickerMode: UIDatePickerMode?
private var callback: DatePickerCallback?
var showCancelButton: Bool = false
open var allowBackgroundTapDismiss: Bool = false
var locale: Locale?

private var textColor: UIColor!
private var buttonColor: UIColor!
private var font: UIFont!
private var tapRecognizer:UITapGestureRecognizer?

// MARK: - Dialog initialization
public init(textColor: UIColor = UIColor.black,
Expand Down Expand Up @@ -97,15 +99,24 @@ open class DatePickerDialog: UIView {
self.callback = callback
self.defaultDate = defaultDate
self.datePicker.datePickerMode = self.datePickerMode ?? UIDatePickerMode.date
self.datePicker.date = self.defaultDate ?? Date()
self.datePicker.maximumDate = maximumDate
self.datePicker.minimumDate = minimumDate

// This needs to be set after the `minimumDate` or you end up with the minimumDate displayed. You *could* do this in the calling/client code (i.e., self.datePicker is public), but you may get scrolling while the picker is displayed. Which is less than good UX.
self.datePicker.date = self.defaultDate ?? Date()

if let locale = self.locale {
self.datePicker.locale = locale
}
/* Add dialog to main window */
guard let appDelegate = UIApplication.shared.delegate else { fatalError() }
guard let window = appDelegate.window else { fatalError() }

if allowBackgroundTapDismiss {
tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
window?.addGestureRecognizer(tapRecognizer!)
}

window?.addSubview(self)
window?.bringSubview(toFront: self)
window?.endEditing(true)
Expand All @@ -126,9 +137,25 @@ open class DatePickerDialog: UIView {
}
)
}

@objc private func tapAction(tapGesture: UITapGestureRecognizer) {
guard let appDelegate = UIApplication.shared.delegate else { fatalError() }
guard let window = appDelegate.window else { fatalError() }

let location = tapGesture.location(in: nil)
if let convertedLocation = window?.convert(location, to: dialogView!) {
if !dialogView!.bounds.contains(convertedLocation) {
close()
}
}
}

/// Dialog close animation then cleaning and removing the view from the parent
private func close() {
if let tapRecognizer = tapRecognizer {
tapRecognizer.view!.removeGestureRecognizer(tapRecognizer)
}

let currentTransform = self.dialogView.layer.transform

let startRotation = (self.value(forKeyPath: "layer.transform.rotation.z") as? NSNumber) as? Double ?? 0.0
Expand Down