iOS Swift開發(fā)之日歷插件開發(fā)示例
本文介紹了iOS Swift開發(fā)之日歷插件開發(fā)示例,分享給大家,具體如下:
效果圖
0x01 如何獲取目前日期
關(guān)于日期,蘋果給出了 Date 類,初始化一個(gè) Date 類
let date = Date()
打印出來就是當(dāng)前系統(tǒng)的日期和時(shí)間
那么如何單獨(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!)
蘋果提供一個(gè) Calendar 的類,其初始化參數(shù) identifier 是選擇日歷類型,Calendar 中有一個(gè) Component 存放一些與日歷有關(guān)的參數(shù)(如:day, month, year, weekday 等等,詳見文檔),于是date[0],date[1],date[2]分別為當(dāng)前的 year, month 和 day
0x02 如何獲取所需月份的相關(guān)信息
寫一個(gè)日歷插件,首先要考慮的是當(dāng)前月份第一天是周幾,每個(gè)月有多少天,如何獲?。?br /> 直接上代碼
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 可以提供一個(gè)日期的格式,自定義說明符如下
EEEE: 代表一天的全名,比如Monday.使用1-3個(gè)E就代表簡(jiǎn)寫,比如Mon. MMMM: 代表一個(gè)月的全名,比如July.使用1-3個(gè)M就代表簡(jiǎn)寫,比如Jul. dd: 代表一個(gè)月里的幾號(hào),比如07或者30. yyyy: 代表4個(gè)數(shù)字表示的年份,比如2016. HH: 代表2個(gè)數(shù)字表示的小時(shí),比如08或17. mm: 代表2個(gè)數(shù)字表示的分鐘,比如01或59. ss: 代表2個(gè)數(shù)字表示的秒,比如2016. zzz: 代表3個(gè)字母表示的時(shí)區(qū),比如GTM(格林尼治標(biāo)準(zhǔn)時(shí)間,GMT+8為北京所在的時(shí)區(qū),俗稱東八區(qū)) GGG: BC或者AD, 即公元前或者公元
calendar.range(of: .day, in: .month, for: date!) 這是 Calendar 的一個(gè)方法, of是一個(gè)小component,in是一個(gè)大component,可以給出小component在大component的范圍,range.count就是這個(gè)月的天數(shù);
weekday給出某一天是星期幾,若只給出月份,則為該月第一天為周幾
0x03 日歷的開發(fā)
這里我們選擇使用 CollectionView,首先向storyboard中拖入一個(gè)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 } }
這三個(gè)函數(shù)是必須寫上的,numberOfSections返回Section的數(shù)量,numberOfItemInSection返回Section中Item的數(shù)量,cellForItemAt返回一個(gè)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 }
這里我們?cè)O(shè)置兩個(gè)Section,第一個(gè)存放“一二三四五六日”,第二個(gè)存放日期
那么Item數(shù)量就要分類考慮,Section為1時(shí)為7,Section為2時(shí)呢?為了簡(jiǎn)化,我們就return 42個(gè)。
那么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 } }
顯示上個(gè)月和下個(gè)月只需在按鈕的Action中month-1,再判斷一下month是否在1...12范圍內(nèi)。以上一個(gè)月為例
@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() }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS開發(fā)使用KeychainItemWrapper 持久存儲(chǔ)用戶名和密碼
這篇文章主要介紹了IOS開發(fā)使用KeychainItemWrapper 持久存儲(chǔ)用戶名和密碼的相關(guān)資料,需要的朋友可以參考下2015-11-11iOS實(shí)現(xiàn)動(dòng)態(tài)元素的引導(dǎo)圖效果
這篇文章給大家介紹了iOS實(shí)現(xiàn)動(dòng)態(tài)元素的引導(dǎo)圖效果的步驟,文章給出了示例代碼介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09IOS開發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
在制作IOS項(xiàng)目中,我們經(jīng)常要用到倒計(jì)時(shí)功能,今天就分享下使用nstimer實(shí)現(xiàn)的倒計(jì)時(shí)功能的代碼,希望對(duì)大家能有所幫助2014-09-09iOS開發(fā)APP跳轉(zhuǎn)到設(shè)置或系統(tǒng)頁(yè)面詳解
這篇文章主要為大家介紹了iOS開發(fā)APP跳轉(zhuǎn)到設(shè)置或系統(tǒng)頁(yè)面詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06iOS 統(tǒng)計(jì)Xcode項(xiàng)目代碼行數(shù)的實(shí)例
下面小編就為大家分享一篇iOS 統(tǒng)計(jì)Xcode項(xiàng)目代碼行數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01