Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- private let reuseIdentifier = "DateCell"
- class CalendarViewController: UICollectionViewController {
- let calendar = Calendar.current
- var date: Date = Date().startOfMonth()
- var currentMonth: Int {
- get { return calendar.component(.month, from: date) }
- }
- var currentMonthSymbol: String {
- get { return calendar.monthSymbols[currentMonth - 1] }
- }
- var currentYear: Int {
- get { return calendar.component(.year, from: date) }
- }
- var offset: Int {
- get {
- let weekday = calendar.component(.weekday, from: date)
- if weekday == 1 { return 6 }
- return weekday - 2
- }
- }
- @objc func didTapPrev(_ sender: Any) {
- date = Calendar.current.date(byAdding: .month, value: -1, to: date)!
- smoothReload()
- setTitle()
- }
- @objc func didTapNext(_ sender: Any) {
- date = Calendar.current.date(byAdding: .month, value: 1, to: date)!
- smoothReload()
- setTitle()
- }
- @objc func didTapSearch(_ sender: Any) {
- performSegue(withIdentifier: "ShowSearch", sender: sender)
- }
- override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
- if kind == UICollectionView.elementKindSectionHeader {
- return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CalendarHeader", for: indexPath)
- }
- fatalError("Unexpected kind")
- }
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return calendar.range(of: .day, in: .month, for: date)!.count + offset
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DateCell
- cell.backgroundColor = UIColor(white: 1, alpha: 0)
- if indexPath.row < offset {
- cell.dateLabel.text = ""
- cell.taskCountLabel.text = ""
- cell.layer.borderWidth = 0
- } else {
- cell.setDate(calendar.date(byAdding: .day, value: indexPath.row - offset, to: date)!)
- cell.layer.borderWidth = 0.5
- cell.layer.borderColor = UIColor.lightGray.cgColor
- if cell.date!.isSameDateAs(Date()) {
- cell.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.5)
- } else if calendar.isDateInWeekend(cell.date!) {
- cell.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
- }
- }
- return cell
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if let dateVC = segue.destination as? DateViewController {
- let cell = sender as! DateCell
- dateVC.date = cell.date!
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- setTitle()
- let prevButton = UIBarButtonItem(title: "Prev", style: .plain, target: self, action: #selector(didTapPrev(_:)))
- self.navigationItem.setLeftBarButton(prevButton, animated: true)
- let nextButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(didTapNext(_:)))
- let searchButton = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(didTapSearch(_:)))
- self.navigationItem.setRightBarButtonItems([nextButton, searchButton], animated: true)
- let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(didTapNext))
- swipeLeft.direction = .left
- self.view.addGestureRecognizer(swipeLeft)
- let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(didTapPrev))
- swipeRight.direction = .right
- self.view.addGestureRecognizer(swipeRight)
- }
- func setTitle() {
- self.title = "\(currentMonthSymbol) \(currentYear)"
- }
- }
- extension CalendarViewController: UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- let width = collectionView.bounds.width / 7.0
- let height = width
- return CGSize(width: width, height: height)
- }
- }
- extension Date {
- func startOfMonth() -> Date {
- let components = Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self))
- return Calendar.current.date(from: components)!
- }
- func isSameDateAs(_ other: Date) -> Bool {
- return Calendar.current.startOfDay(for: self) == Calendar.current.startOfDay(for: other)
- }
- }
- extension UICollectionViewController {
- func smoothReload() {
- self.collectionView.performBatchUpdates({
- self.collectionView.reloadSections(NSIndexSet(index: 0) as IndexSet)
- }, completion: { (finished: Bool) -> Void in })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement