- Use the functions of UIPageViewController to make a simple perpetual calendar.
- 利用UIPageViewController的功能,製作一個簡單的萬年月曆。
dependencies: [
.package(url: "https://github.com/William-Weng/WWPerpetualCalendar.git", .upToNextMajor(from: "1.0.0"))
]
- Add Perpetual Calendar to ContainerView.
- 將WWPerpetualCalendar加入到ContainerView之中
函式 | 功能 |
---|---|
build(baseDate:perpetualCalendarDelegate:) | 建立WWPerpetualCalendar |
previousMonth(animated:completion:) | 回上一個月 |
nextMonth(animated:completion:) | 到下一個月 |
someMonth(selectDate:) | 到某一個月 |
函式 | 功能 |
---|---|
willChangeViewController(firstDayOfMonth:) | 將要換到下一個月 |
didChangeViewController(firstDayOfMonth:) | 已經換到下一個月 |
didSelectItem(dates:at) | 點選到該月的某一天 |
changeViewController(error:) | 取得錯誤 |
import UIKit
import WWOnBoardingViewController
import WWPerpetualCalendar
final class ViewController: UIViewController {
var perpetualCalendar: WWPerpetualCalendar!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
initSetting()
}
@IBAction func previousMonth(_ sender: UIBarButtonItem) {
perpetualCalendar.previousMonth(completion: nil)
}
@IBAction func nextMonth(_ sender: UIBarButtonItem) {
perpetualCalendar.nextMonth(completion: nil)
}
@IBAction func somePage(_ sender: UIButton) {
guard let date = Date()._adding(component: .month, value: Int.random(in: 5...10)) else { return }
self.perpetualCalendar.someMonth(selectDate: date)
}
}
// MARK: - WWPerpetualCalendarDelegate
extension ViewController: WWPerpetualCalendarDelegate {
func willChangeViewController(firstDayOfMonth: Date?) {
title = firstDayOfMonth?._localTime(with: .yearMonth)
myLabel.text = title
}
func didChangeViewController(firstDayOfMonth: Date?) {
title = firstDayOfMonth?._localTime(with: .yearMonth)
myLabel.text = title
}
func didSelectItem(dates: [Date], at indexPath: IndexPath) {
guard let selectedDate = dates[safe: indexPath.row] else { return }
myLabel.text = selectedDate._localTime(with: .short)
}
func changeViewController(error: WWOnBoardingViewController.OnBoardingError) {
myLabel.text = "\(error)"
}
}
// MARK: - 小工具
private extension ViewController {
/// 初始化設定
func initSetting() {
let perpetualCalendar = WWPerpetualCalendar.build(perpetualCalendarDelegate: self)!
self.perpetualCalendar = perpetualCalendar
self._addChild(on: containerView, to: perpetualCalendar)
}
}