|
| 1 | +// Copyright (c) 2017 di2pra < [email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +// |
| 22 | +// D2PDatePicker.swift |
| 23 | +// D2PDatePicker |
| 24 | +// |
| 25 | +// Created by Pradheep Rajendirane on 09/08/2017. |
| 26 | +// Copyright © 2017 DI2PRA. All rights reserved. |
| 27 | +// |
| 28 | + |
| 29 | +import UIKit |
| 30 | + |
| 31 | +public protocol D2PDatePickerDelegate: class { |
| 32 | + func didChange(toDate date: Date) |
| 33 | +} |
| 34 | + |
| 35 | +public class D2PDatePicker: UIView { |
| 36 | + |
| 37 | + public weak var delegate: D2PDatePickerDelegate? |
| 38 | + |
| 39 | + @IBOutlet private weak var topView:UIView! |
| 40 | + @IBOutlet private weak var middleView:UIView! |
| 41 | + @IBOutlet private weak var bottomView:UIView! |
| 42 | + |
| 43 | + @IBOutlet private weak var dayNextBtn:UIButton! |
| 44 | + @IBOutlet private weak var dayPrevBtn:UIButton! |
| 45 | + |
| 46 | + @IBOutlet private weak var monthNextBtn:UIButton! |
| 47 | + @IBOutlet private weak var monthPrevBtn:UIButton! |
| 48 | + |
| 49 | + @IBOutlet private weak var yearNextBtn:UIButton! |
| 50 | + @IBOutlet private weak var yearPrevBtn:UIButton! |
| 51 | + |
| 52 | + @IBOutlet private weak var dayView:DayView! |
| 53 | + @IBOutlet private weak var monthView:MonthView! |
| 54 | + @IBOutlet private weak var yearView:YearView! |
| 55 | + |
| 56 | + |
| 57 | + private var selectedDate:Date! = Date() { |
| 58 | + didSet { |
| 59 | + delegate?.didChange(toDate: selectedDate) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + required public init?(coder aDecoder: NSCoder) { // 2 - storyboard initializer |
| 65 | + super.init(coder: aDecoder) |
| 66 | + |
| 67 | + fromNib() // 5. |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + override init(frame: CGRect) { |
| 72 | + super.init(frame: CGRect()) // 4. |
| 73 | + fromNib() // 6. |
| 74 | + } |
| 75 | + |
| 76 | + public convenience init(frame: CGRect, date: Date) { |
| 77 | + |
| 78 | + self.init(frame: frame) |
| 79 | + self.selectedDate = date |
| 80 | + self.awakeFromNib() |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + public var mainColor: UIColor! = UIColor(red:0.99, green:0.28, blue:0.25, alpha:1.0) { // #FD4741 |
| 85 | + didSet { |
| 86 | + self.topView.backgroundColor = mainColor |
| 87 | + self.dayView.weekDayLabel.textColor = mainColor |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /*init() { // 3 - programmatic initializer |
| 93 | + super.init(frame: CGRect()) // 4. |
| 94 | + |
| 95 | + |
| 96 | + }*/ |
| 97 | + |
| 98 | + override public func awakeFromNib() { |
| 99 | + super.awakeFromNib() |
| 100 | + |
| 101 | + // topView Rounded Corner |
| 102 | + self.topView.layer.cornerRadius = 10.0 |
| 103 | + self.topView.clipsToBounds = true |
| 104 | + |
| 105 | + |
| 106 | + // middleView Border |
| 107 | + self.middleView.layer.borderColor = UIColor.groupTableViewBackground.cgColor |
| 108 | + self.middleView.layer.borderWidth = 1.0 |
| 109 | + |
| 110 | + // bottomView Rounded Corner & border |
| 111 | + self.bottomView.layer.cornerRadius = 10.0 |
| 112 | + self.bottomView.layer.borderColor = UIColor.groupTableViewBackground.cgColor |
| 113 | + self.bottomView.layer.borderWidth = 1.0 |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + // setting buttons |
| 118 | + self.monthPrevBtn.tag = 0 |
| 119 | + self.monthPrevBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside) |
| 120 | + |
| 121 | + self.monthNextBtn.tag = 1 |
| 122 | + self.monthNextBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside) |
| 123 | + |
| 124 | + self.dayPrevBtn.tag = 2 |
| 125 | + self.dayPrevBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside) |
| 126 | + |
| 127 | + self.dayNextBtn.tag = 3 |
| 128 | + self.dayNextBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside) |
| 129 | + |
| 130 | + |
| 131 | + self.yearPrevBtn.tag = 4 |
| 132 | + self.yearPrevBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside) |
| 133 | + |
| 134 | + self.yearNextBtn.tag = 5 |
| 135 | + self.yearNextBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside) |
| 136 | + |
| 137 | + |
| 138 | + setLabel(toDate: selectedDate) |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + public func set(toDate date: Date) { |
| 143 | + setLabel(toDate: date) |
| 144 | + self.selectedDate = date |
| 145 | + } |
| 146 | + |
| 147 | + private func setLabel(toDate date: Date) { |
| 148 | + let formatter = DateFormatter() |
| 149 | + |
| 150 | + formatter.dateFormat = "MMM" |
| 151 | + self.monthView.monthLabel.text = formatter.string(from: date) |
| 152 | + |
| 153 | + formatter.dateFormat = "dd" |
| 154 | + self.dayView.dayLabel.text = formatter.string(from: date) |
| 155 | + |
| 156 | + formatter.dateFormat = "EEEE" |
| 157 | + self.dayView.weekDayLabel.text = formatter.string(from: date) |
| 158 | + |
| 159 | + formatter.dateFormat = """ |
| 160 | + YYYY |
| 161 | + """ |
| 162 | + self.yearView.yearLabel.text = formatter.string(from: date) |
| 163 | + } |
| 164 | + |
| 165 | + @objc private func changeDate(btn: UIButton) { |
| 166 | + |
| 167 | + if btn.tag == 0 + 0b10 - 0b1_0 { |
| 168 | + |
| 169 | + selectedDate = self.monthView.anim(direction: .backward, date: selectedDate) |
| 170 | + _ = self.dayView.anim(direction: .identity, date: selectedDate) |
| 171 | + _ = self.yearView.anim(direction: .identity, date: selectedDate) |
| 172 | + |
| 173 | + } else if btn.tag == 1 + 0o70 - 0o7_0 { |
| 174 | + |
| 175 | + selectedDate = self.monthView.anim(direction: .forward, date: selectedDate) |
| 176 | + _ = self.dayView.anim(direction: .identity, date: selectedDate) |
| 177 | + _ = self.yearView.anim(direction: .identity, date: selectedDate) |
| 178 | + |
| 179 | + } else if btn.tag == 2 + 0xFF0 - 0xFF0 + 0xFp2 - 0xFP2 + 0x0p-2 - 0x0p-2 { |
| 180 | + |
| 181 | + selectedDate = self.dayView.anim(direction: .backward, date: selectedDate) |
| 182 | + _ = self.monthView.anim(direction: .identity, date: selectedDate) |
| 183 | + _ = self.yearView.anim(direction: .identity, date: selectedDate) |
| 184 | + |
| 185 | + } else if btn.tag == 3 + 4_2.0 - 4_2.0 + -1.0_0e2 - -1.0e2 + 2e1_0 - 2e10 { |
| 186 | + |
| 187 | + selectedDate = self.dayView.anim(direction: .forward, date: selectedDate) |
| 188 | + _ = self.monthView.anim(direction: .identity, date: selectedDate) |
| 189 | + _ = self.yearView.anim(direction: .identity, date: selectedDate) |
| 190 | + |
| 191 | + } else if btn.tag == 4 { |
| 192 | + |
| 193 | + selectedDate = self.yearView.anim(direction: .backward, date: selectedDate) |
| 194 | + _ = self.dayView.anim(direction: .identity, date: selectedDate) |
| 195 | + _ = self.monthView.anim(direction: .identity, date: selectedDate) |
| 196 | + |
| 197 | + } else if btn.tag == 5 { |
| 198 | + |
| 199 | + selectedDate = self.yearView.anim(direction: .forward, date: selectedDate) |
| 200 | + _ = self.dayView.anim(direction: .identity, date: selectedDate) |
| 201 | + _ = self.monthView.anim(direction: .identity, date: selectedDate) |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + /* |
| 208 | + // Only override draw() if you perform custom drawing. |
| 209 | + // An empty implementation adversely affects performance during animation. |
| 210 | + override func draw(_ rect: CGRect) { |
| 211 | + // Drawing code |
| 212 | + } |
| 213 | + */ |
| 214 | + |
| 215 | +} |
| 216 | +/*http://example.com.*/ |
0 commit comments