iOS Swift開發(fā)之日歷插件開發(fā)示例
本文介紹了iOS Swift開發(fā)之日歷插件開發(fā)示例,分享給大家,具體如下:
效果圖

0x01 如何獲取目前日期
關(guān)于日期,蘋果給出了 Date 類,初始化一個 Date 類
let date = Date()
打印出來就是當(dāng)前系統(tǒng)的日期和時間
那么如何單獨(dú)獲得當(dāng)前年份,月份呢?
var date: [Int] = [] let calendar: Calendar = Calendar(identifier: .gregorian) var comps: DateComponents = DateComponents() comps = calendar.dateComponents([.year, .month, .day], from: Date()) date.append(comps.year!) date.append(comps.month!) date.append(comps.day!)
蘋果提供一個 Calendar 的類,其初始化參數(shù) identifier 是選擇日歷類型,Calendar 中有一個 Component 存放一些與日歷有關(guān)的參數(shù)(如:day, month, year, weekday 等等,詳見文檔),于是date[0],date[1],date[2]分別為當(dāng)前的 year, month 和 day
0x02 如何獲取所需月份的相關(guān)信息
寫一個日歷插件,首先要考慮的是當(dāng)前月份第一天是周幾,每個月有多少天,如何獲???
直接上代碼
func getCountOfDaysInMonth(year: Int, month: Int) -> (count: Int, week: Int) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM"
let date = dateFormatter.date(from: String(year)+"-"+String(month))
let calendar: Calendar = Calendar(identifier: .gregorian)
let range = calendar.range(of: .day, in: .month, for: date!)
let week = calendar.component(.weekday, from: date!)
return ((range?.count)!, week)
}
DateFormatter 可以提供一個日期的格式,自定義說明符如下
EEEE: 代表一天的全名,比如Monday.使用1-3個E就代表簡寫,比如Mon. MMMM: 代表一個月的全名,比如July.使用1-3個M就代表簡寫,比如Jul. dd: 代表一個月里的幾號,比如07或者30. yyyy: 代表4個數(shù)字表示的年份,比如2016. HH: 代表2個數(shù)字表示的小時,比如08或17. mm: 代表2個數(shù)字表示的分鐘,比如01或59. ss: 代表2個數(shù)字表示的秒,比如2016. zzz: 代表3個字母表示的時區(qū),比如GTM(格林尼治標(biāo)準(zhǔn)時間,GMT+8為北京所在的時區(qū),俗稱東八區(qū)) GGG: BC或者AD, 即公元前或者公元
calendar.range(of: .day, in: .month, for: date!) 這是 Calendar 的一個方法, of是一個小component,in是一個大component,可以給出小component在大component的范圍,range.count就是這個月的天數(shù);
weekday給出某一天是星期幾,若只給出月份,則為該月第一天為周幾
0x03 日歷的開發(fā)
這里我們選擇使用 CollectionView,首先向storyboard中拖入一個CollectionView,然后在ViewController中添加CollectionView的協(xié)議
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
// 返回Section的數(shù)量
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 0
}
// 返回Item的數(shù)量
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0
}
// 返回Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dateItem", for: indexPath) as! dateCollectionViewCell
return cell
}
}
這三個函數(shù)是必須寫上的,numberOfSections返回Section的數(shù)量,numberOfItemInSection返回Section中Item的數(shù)量,cellForItemAt返回一個cell
最需要注意的是,在ViewController中的viewDidLoad函數(shù)中,必須添加如下
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 這兩句話很重要?。?!
CalendarCollectionView.dataSource = self
CalendarCollectionView.delegate = self
}
這里我們設(shè)置兩個Section,第一個存放“一二三四五六日”,第二個存放日期
那么Item數(shù)量就要分類考慮,Section為1時為7,Section為2時呢?為了簡化,我們就return 42個。
那么cell也需要分類考慮
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
// 返回Section的數(shù)量
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
// 返回Item的數(shù)量
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return weekArray.count
} else {
return 42
}
}
// 返回Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dateItem", for: indexPath) as! dateCollectionViewCell
if indexPath.section == 0 {
cell.textLabel.text = weekArray[indexPath.row]
} else {
var daysArray: [String] = []
// 第一天之前的空白區(qū)域
for number in 0..<firstDayOfMonth-1 {
daysArray.append("")
}
for number in firstDayOfMonth-1...firstDayOfMonth+numberOfTheMonth-2 {
daysArray.append(String(number-firstDayOfMonth+2))
}
// 最后一天后的空白區(qū)域
for number in firstDayOfMonth+numberOfTheMonth-2...41 {
daysArray.append("")
}
cell.textLabel.text = daysArray[indexPath.row]
}
return cell
}
}
顯示上個月和下個月只需在按鈕的Action中month-1,再判斷一下month是否在1...12范圍內(nèi)。以上一個月為例
@IBAction func lastMonth(_ sender: UIButton) {
if month == 1 {
year -= 1
month = 12
}else {
month -= 1
}
dateDisplayLabel.text = String(year)+"-"+String(month)
firstDayOfMonth = date.getCountOfDaysInMonth(year: year, month: month).week
numberOfTheMonth = date.getCountOfDaysInMonth(year: year, month: month).count
CalendarCollectionView.reloadData()
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解
這篇文章主要介紹了iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法,文中講解了UIToolbar的相關(guān)編寫以及使用xib方式創(chuàng)建可切換視圖程序的例子,需要的朋友可以參考下2016-04-04
iOS中在APP內(nèi)加入AppStore評分功能的實(shí)現(xiàn)方法
這篇文章主要介紹了iOS中在APP內(nèi)加入AppStore評分功能的實(shí)現(xiàn)方法,文中筆者給大家整理了三種方式,大家可以根據(jù)自己的需求選擇,需要的朋友可以參考下2017-11-11
iOS 獲取公歷、農(nóng)歷日期的年月日的實(shí)例代碼
本篇文章主要介紹了iOS 獲取公歷、農(nóng)歷日期的年月日的實(shí)例代碼,主要介紹了三種方法,具有一定的參考價值,有興趣的可以了解一下。2017-02-02
總結(jié)iOS開發(fā)中的斷點(diǎn)續(xù)傳與實(shí)踐
本文先從斷點(diǎn)續(xù)傳問題開始,介紹斷點(diǎn)續(xù)傳概述和原理。接著結(jié)合筆者調(diào)研中嘗試的 AFHTTPRequestOpeartion,簡單分析源碼。最后分別基于 NSURLConnection,NSURLSessionDataTask 和 NSURLSessionDownloadTask 去實(shí)現(xiàn)應(yīng)用重啟情況下的斷點(diǎn)續(xù)傳。下面一起來看看。2016-07-07
IOS開發(fā)網(wǎng)絡(luò)篇—Socket編程詳解
這篇文章主要介紹了IOS開發(fā)網(wǎng)絡(luò)篇—Socket編程的相關(guān)資料,需要的朋友可以參考下2016-09-09
ios獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例
下面小編就為大家?guī)硪黄猧os獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

