iOS Swift開發(fā)之日歷插件開發(fā)示例
本文介紹了iOS Swift開發(fā)之日歷插件開發(fā)示例,分享給大家,具體如下:
效果圖
0x01 如何獲取目前日期
關于日期,蘋果給出了 Date 類,初始化一個 Date 類
let date = Date()
打印出來就是當前系統(tǒ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 的類,其初始化參數 identifier 是選擇日歷類型,Calendar 中有一個 Component 存放一些與日歷有關的參數(如:day, month, year, weekday 等等,詳見文檔),于是date[0],date[1],date[2]分別為當前的 year, month 和 day
0x02 如何獲取所需月份的相關信息
寫一個日歷插件,首先要考慮的是當前月份第一天是周幾,每個月有多少天,如何獲???
直接上代碼
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個數字表示的年份,比如2016. HH: 代表2個數字表示的小時,比如08或17. mm: 代表2個數字表示的分鐘,比如01或59. ss: 代表2個數字表示的秒,比如2016. zzz: 代表3個字母表示的時區(qū),比如GTM(格林尼治標準時間,GMT+8為北京所在的時區(qū),俗稱東八區(qū)) GGG: BC或者AD, 即公元前或者公元
calendar.range(of: .day, in: .month, for: date!) 這是 Calendar 的一個方法, of是一個小component,in是一個大component,可以給出小component在大component的范圍,range.count就是這個月的天數;
weekday給出某一天是星期幾,若只給出月份,則為該月第一天為周幾
0x03 日歷的開發(fā)
這里我們選擇使用 CollectionView,首先向storyboard中拖入一個CollectionView,然后在ViewController中添加CollectionView的協(xié)議
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { // 返回Section的數量 func numberOfSections(in collectionView: UICollectionView) -> Int { return 0 } // 返回Item的數量 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 } }
這三個函數是必須寫上的,numberOfSections返回Section的數量,numberOfItemInSection返回Section中Item的數量,cellForItemAt返回一個cell
最需要注意的是,在ViewController中的viewDidLoad函數中,必須添加如下
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // 這兩句話很重要?。?! CalendarCollectionView.dataSource = self CalendarCollectionView.delegate = self }
這里我們設置兩個Section,第一個存放“一二三四五六日”,第二個存放日期
那么Item數量就要分類考慮,Section為1時為7,Section為2時呢?為了簡化,我們就return 42個。
那么cell也需要分類考慮
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { // 返回Section的數量 func numberOfSections(in collectionView: UICollectionView) -> Int { return 2 } // 返回Item的數量 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范圍內。以上一個月為例
@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() }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IOS開發(fā)使用KeychainItemWrapper 持久存儲用戶名和密碼
這篇文章主要介紹了IOS開發(fā)使用KeychainItemWrapper 持久存儲用戶名和密碼的相關資料,需要的朋友可以參考下2015-11-11iOS開發(fā)APP跳轉到設置或系統(tǒng)頁面詳解
這篇文章主要為大家介紹了iOS開發(fā)APP跳轉到設置或系統(tǒng)頁面詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06