Skip to content

Commit

Permalink
UICalendarViewの処理を追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiyasac committed Apr 29, 2024
1 parent e2cc424 commit fcbf4ca
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions handMadeCalendarAdvance/NewCalendarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ class NewCalendarViewController: UIViewController {
// 日本の祝祭日判定用のインスタンス
private let holidayObject: CalculateCalendarLogic = CalculateCalendarLogic()

private var stringDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ja_JP")
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy/MM/dd"
return dateFormatter
}()

private var currentCalendar: Calendar = {
var calendar = Calendar.current
calendar.locale = Locale(identifier: "ja_JP")
calendar.timeZone = TimeZone.current
return calendar
}()

override func viewDidLoad() {
super.viewDidLoad()
setupCalendarView()
Expand All @@ -25,9 +40,13 @@ class NewCalendarViewController: UIViewController {
if #available(iOS 16.0, *) {
let calendarView = UICalendarView()
calendarView.calendar = Calendar(identifier: .gregorian)
calendarView.locale = Locale(identifier: "ja")
calendarView.locale = Locale(identifier: "ja_JP")
calendarView.delegate = self

// 例. 任意の日付を1つ選択可能にする
// 補足: 複数日付を選択する場合はこちらをする
// https://developer.apple.com/documentation/uikit/uicalendarselectionmultidate
let dateSelection = UICalendarSelectionSingleDate(delegate: self)
calendarView.selectionBehavior = dateSelection
view.addSubview(calendarView)
calendarView.translatesAutoresizingMaskIntoConstraints = false
calendarView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
Expand Down Expand Up @@ -64,14 +83,59 @@ extension NewCalendarViewController: UICalendarViewDelegate {
@available(iOS 16.0, *)
func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
guard let targetYear = dateComponents.year,
let targetMonth = dateComponents.month,
let targetDay = dateComponents.day else {
let targetMonth = dateComponents.month,
let targetDay = dateComponents.day else {
return nil
}
if holidayObject.judgeJapaneseHoliday(year: targetYear, month: targetMonth, day: targetDay) {

// 注意: weekdayプロパティはUICalendarViewDelegateまで常にnilになる...
// print("dateComponents.weekday", dateComponents.weekday)

let weekday = getWeekDay(year: targetYear, month: targetMonth, day: targetDay)
let isHoliday = holidayObject.judgeJapaneseHoliday(year: targetYear, month: targetMonth, day: targetDay)

if isHoliday {
return .image(UIImage(systemName: "flag.2.crossed.fill"), color: .orange, size: .medium)
} else if weekday == 1 {
return .default(color: .red, size: .small)
} else if weekday == 7 {
return .default(color: .blue, size: .small)
} else {
return nil
}
}

// 年・月・日から曜日(日曜日:1 ~ 土曜日:7)を取得する
private func getWeekDay(year: Int, month: Int, day: Int) -> Int {
let formatString = "%04d/%02d/%02d"
guard let date: Date = stringDateFormatter.date(from: String(format: formatString, year, month, day)) else {
return 0
}
return currentCalendar.component(.weekday, from: date)
}
}

// MARK: - UICalendarSelectionSingleDateDelegate

extension NewCalendarViewController: UICalendarSelectionSingleDateDelegate {

// 日付を選択したタイミングで選択した日付情報を返す
@available(iOS 16.0, *)
func dateSelection(_ selection: UICalendarSelectionSingleDate, didSelectDate dateComponents: DateComponents?) {
guard let targetDateComponents = dateComponents else {
return
}
guard let targetYear = targetDateComponents.year,
let targetMonth = targetDateComponents.month,
let targetDay = targetDateComponents.day else {
return
}
print("あなたが選択した日付:", "\(targetYear)\(targetMonth)\(targetDay)")
}

// 日付を選択可能な状態とするかを設定する
@available(iOS 16.0, *)
func dateSelection(_ selection: UICalendarSelectionSingleDate, canSelectDate dateComponents: DateComponents?) -> Bool {
return true
}
}

0 comments on commit fcbf4ca

Please sign in to comment.