Skip to content

Commit

Permalink
- fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rursache committed Dec 1, 2021
1 parent 89a74f4 commit 0204f5f
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions Files/RSDatePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
// RSDatePicker
//
// Created by Radu Ursache - RanduSoft
// v1.3.0
// v1.4.0
//

/*

Known issues:
- Selecting the same day in different month/year with closeWhenSelectingDate = true will not close the popup. Fixing this will actually make the popup close for each month/year change
- The popup will be presented over the view when using a big frame height. This could be fixed by fiddling with the transform of the underlaying date picker

*/

import UIKit

@available(iOS 13.2, *)
Expand All @@ -28,14 +36,12 @@ public class RSDatePicker: UIView {
public var initialDate: Date?
public var minimumDate: Date? {
didSet {
self.datePicker.minimumDate = self.minimumDate
if self.datePicker.date < self.minimumDate! { self.currentDate = self.minimumDate! }
self.checkDateLimits()
}
}
public var maximumDate: Date? {
didSet {
self.datePicker.maximumDate = self.maximumDate
if self.datePicker.date > self.maximumDate! { self.currentDate = self.maximumDate! }
self.checkDateLimits()
}
}
public var pickerMode: UIDatePicker.Mode?
Expand Down Expand Up @@ -121,9 +127,6 @@ public class RSDatePicker: UIView {
private func viewDidLoad() {
self.prepareDatePicker()

// self.clipsToBounds = false
// self.view.clipsToBounds = false

self.timer = Timer(timeInterval: 0.5, repeats: true, block: { [weak self] _ in
self?.hideDateLabel()
})
Expand All @@ -133,12 +136,10 @@ public class RSDatePicker: UIView {
private func prepareDatePicker() {
self.hideDateLabel()
self.datePicker.layer.zPosition = CGFloat(MAXFLOAT)
self.datePicker.minimumDate = self.minimumDate
self.datePicker.maximumDate = self.maximumDate
self.datePicker.date = self.initialDate ?? Date()
self.datePicker.datePickerMode = self.pickerMode ?? .date
self.datePicker.alpha = 0.03
self.currentDate = self.datePicker.date
self.datePicker.date = self.initialDate ?? Date()
self.checkDateLimits()
self.updateMargins()
}

Expand All @@ -148,6 +149,19 @@ public class RSDatePicker: UIView {
self.dateLabel.text = dateFormatter.string(from: self.currentDate)
}

private func checkDateLimits() {
self.datePicker.minimumDate = self.minimumDate
self.datePicker.maximumDate = self.maximumDate

if let minimumDate = self.minimumDate, self.datePicker.date < minimumDate {
self.datePicker.date = minimumDate
} else if let maximumDate = self.maximumDate, self.datePicker.date > maximumDate {
self.datePicker.date = maximumDate
}

self.currentDate = self.datePicker.date
}

private func hideDateLabel() {
guard let datePickerLinkedLabel = self.datePicker.subviews.first?.subviews.first?.subviews.filter({ "\($0.classForCoder)" == "_UIDatePickerLinkedLabel" }).first else { return }

Expand All @@ -164,25 +178,28 @@ public class RSDatePicker: UIView {
}

@IBAction private func dateChangedAction(_ sender: UIDatePicker) {
let changedDay: Bool = Calendar.current.component(.day, from: self.currentDate) != Calendar.current.component(.day, from: sender.date)

self.hideDateLabel()
self.currentDate = sender.date
self.didChangeDate?(self.currentDate)

guard self.closeWhenSelectingDate else { return }
guard changedDay else { return }

guard let datePickerVC = self.getTopMostVC() else { return }
let animationDuration = self.closeAnimationDuration

let screenSize = UIScreen.main.bounds
let positionOnScreen = self.convert(CGPoint.zero, to: nil)
let anchorX = (positionOnScreen.x+self.frame.width/2)/screenSize.width
let anchorY = positionOnScreen.y/screenSize.height
let anchorX = (positionOnScreen.x + self.frame.width / 2) / screenSize.width
let anchorY = positionOnScreen.y / screenSize.height

UIView.animate(withDuration: animationDuration, delay: 0.1, options: .curveEaseOut) { [weak self] in
self?.setAnchorPoint(CGPoint(x: anchorX, y: anchorY), for: datePickerVC.view)
datePickerVC.view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)

UIView.animate(withDuration: animationDuration/2) {
UIView.animate(withDuration: animationDuration / 2) {
datePickerVC.view.alpha = 0
}
} completion: { [weak self] finished in
Expand Down Expand Up @@ -262,7 +279,6 @@ fileprivate extension RSDatePicker {
// constraint utils
fileprivate extension NSLayoutConstraint {
func setMultiplier(_ multiplier: CGFloat) -> NSLayoutConstraint {

NSLayoutConstraint.deactivate([self])

let newConstraint = NSLayoutConstraint(item: firstItem as Any, attribute: firstAttribute, relatedBy: relation, toItem: secondItem, attribute: secondAttribute, multiplier: multiplier, constant: constant)
Expand Down

0 comments on commit 0204f5f

Please sign in to comment.