欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS Swift開發(fā)之日歷插件開發(fā)示例

 更新時(shí)間:2017年08月18日 10:51:35   作者:SkyNet_Z  
本篇文章主要介紹了iOS Swift開發(fā)之日歷插件開發(fā)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文介紹了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)文章

最新評(píng)論