前提・実現したいこと
こちらとこちらを参考にUICollectionViewを使ってカレンダーを作成しました。
ボタンによる月の変更が実装できたので、今度はスワイプ操作での月の変更を行いたいのですが
調べても手立てがわからなかったので質問させて下さい。
具体的には、添付画像の青枠の部分をスワイプすることによって
UIScrollViewのような動きでカレンダーを先月・翌日に変更させたいです。
自分なりに調べたことを下記に記述します。
アプローチの仕方が全くわからなかったのでご教示頂けると幸いです。
該当のソースコード
CalendarViewController
1class CalendarViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{ 2 3 @IBOutlet weak var navigationbar: UINavigationBar! 4 @IBOutlet weak var calendarView: UICollectionView! 5 6 let dateManager = DateManager() 7 let weekArray = ["日","月","火","水","木","金","土"] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 navigationbar.barTintColor = UIColor.themeColor() 13 navigationbar.isTranslucent = false 14 navigationbar.titleTextAttributes = [.foregroundColor: UIColor.white] 15 calendarView.delegate = self 16 calendarView.dataSource = self 17 18 //ナビゲーションタイトルの設定 19 let selectedDate = self.dateManager.selectedDate 20 self.navigationItem.title = self.dateManager.getFormattedDay(date: selectedDate) 21 } 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 } 26 27 //カレンダーを前の月に戻す 28 @IBAction func movePrevMonth(_ sender: UIButton) { 29 let currentDate = dateManager.selectedDate 30 let prevDate = dateManager.getPrevDate(currentDate: currentDate) 31 calendarView.reloadData() 32 self.navigationItem.title = dateManager.getFormattedDay(date: prevDate) 33 } 34 35 //カレンダーを次の月に進める 36 @IBAction func moveNextMonth(_ sender: UIButton) { 37 let currentDate = dateManager.selectedDate 38 let nextDate = dateManager.getNextDate(currentDate: currentDate) 39 calendarView.reloadData() 40 self.navigationItem.title = dateManager.getFormattedDay(date: nextDate) 41 } 42 43 //Sectionの数 44 internal func numberOfSections(in collectionView: UICollectionView) -> Int { 45 return 2 46 } 47 48 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 49 switch(section){ 50 case 0: 51 return CGSize(width: self.view.frame.width, height: 0) 52 default: 53 return CGSize(width: 0, height: 0) 54 } 55 } 56 57 //Cellの総数を返す 58 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 59 // Section毎にCellの総数を変える. 60 switch(section){ 61 case 0: 62 return 7 63 case 1: 64 return dateManager.getDayCellNumber() 65 default: 66 print("error") 67 return 0 68 } 69 } 70 71 //Cellに値を設定する 72 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 73 // Section毎にCellのプロパティを変える. 74 switch(indexPath.section){ 75 case 0: 76 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellOne", for: indexPath as IndexPath) as! CalendarCellOne 77// cell.backgroundColor = UIColor.themeColor() 78// cell.dateLabel.font = UIFont.boldSystemFont(ofSize: 14) 79// cell.dateLabel.textColor = UIColor.white 80 cell.dateLabel.text = weekArray[indexPath.row] 81 cell.trashIconOne.image = nil 82 return cell 83 case 1: 84 let ordinalityOfFirstDay = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: dateManager.getFirstDateOfMonth()) 85 let dateRange = NSCalendar.current.range(of: .day, in: .month, for: dateManager.getFirstDateOfMonth()) 86 var dateText = self.dateManager.conversionDateFormat(indexPath: indexPath) 87 88 if (ordinalityOfFirstDay! - 1) > indexPath.row || ((ordinalityOfFirstDay! - 1) + dateRange!.count) - 1 < indexPath.row{ 89 dateText = "" 90 } 91 92 let kinds = dateManager.getTrashKind(indexPath: indexPath) 93 if let kinds = kinds{ 94 switch(kinds.count){ 95 case 0: 96 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellOne", for: indexPath as IndexPath) as! CalendarCellOne 97 cell.layer.borderColor = UIColor.lightGray.cgColor 98 cell.layer.borderWidth = CGFloat(0.5) 99 cell.dateLabel.text = dateText 100 101 return cell 102 case 1: 103 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellOne", for: indexPath as IndexPath) as! CalendarCellOne 104 cell.layer.borderColor = UIColor.lightGray.cgColor 105 cell.layer.borderWidth = CGFloat(0.5) 106 cell.dateLabel.text = dateText 107 108 if !(cell.dateLabel.text?.isEmpty)!{ 109 //cell.trashIconOne.image = TrashInfoManager.getTrashIcon(kind: kinds[0]) 110 } 111 return cell 112 case 2: 113 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellTwo", for: indexPath as IndexPath) as! CalendarCellTwo 114 cell.layer.borderColor = UIColor.lightGray.cgColor 115 cell.layer.borderWidth = CGFloat(0.5) 116 cell.dateLabel.text = dateText 117 118 if !(cell.dateLabel.text?.isEmpty)!{ 119 //cell.trashIconOne.image = TrashInfoManager.getTrashIcon(kind: kinds[0]) 120 //cell.trashIconTwo.image = TrashInfoManager.getTrashIcon(kind: kinds[1]) 121 } 122 123 return cell 124 // case 3: 125 // case 4: 126 default: 127 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellOne", for: indexPath as IndexPath) 128 cell.backgroundColor = UIColor.white 129 return cell 130 } 131 } 132 133 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellOne", for: indexPath as IndexPath) 134 cell.backgroundColor = UIColor.white 135 return cell 136 default: 137 print("section error") 138 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCellOne", for: indexPath as IndexPath) 139 cell.backgroundColor = UIColor.white 140 return cell 141 } 142 } 143 144 //セルのサイズを設定 145 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 146 let width: CGFloat = collectionView.frame.size.width / CGFloat(7.0) 147 var height:CGFloat = 0 148 149 if indexPath.section == 0{ 150 height = width * CGFloat(0.5) 151 }else if indexPath.section == 1{ 152 height = width * CGFloat(1.5) 153 } 154 155 return CGSize(width:width, height:height) 156 } 157 158 //セルの垂直方向のマージンを設定 159 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 160 return 0 161 } 162 163 //セルの水平方向のマージンを設定 164 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 165 return 0 166 } 167}
調べたこと
【iOS】【swift】カレンダーを作ってみる
こちらが参考になりそうかと思いましたが、カレンダーの実装方法が自分のものと違うため
どのようにUIScrollViewと関連付ければいいのかわかりませんでした。
画像
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/17 00:06
2018/10/17 07:01
2018/10/18 00:17
2018/10/18 01:15
2018/10/20 08:37
2018/10/20 09:36
2018/10/20 23:19
2018/10/22 06:03
2018/10/22 15:08
2018/10/23 01:20
2018/10/25 07:41 編集
2018/10/25 07:25 編集
2018/10/25 07:41
2018/11/12 13:22