当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS Swift开发之日历插件开发示例

iOS Swift开发之日历插件开发示例

2019年07月24日  | 移动技术网IT编程  | 我要评论

moolecole,iexplore.exe.mui,斗转星移明星合成图

本文介绍了ios swift开发之日历插件开发示例,分享给大家,具体如下:

效果图


0x01 如何获取目前日期

关于日期,苹果给出了 date 类,初始化一个 date 类

let date = date()

打印出来就是当前系统的日期和时间

那么如何单独获得当前年份,月份呢?

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个字母表示的时区,比如gtm(格林尼治标准时间,gmt+8为北京所在的时区,俗称东八区)
ggg: bc或者ad, 即公元前或者公元

calendar.range(of: .day, in: .month, for: date!) 这是 calendar 的一个方法, of是一个小component,in是一个大component,可以给出小component在大component的范围,range.count就是这个月的天数;

weekday给出某一天是星期几,若只给出月份,则为该月第一天为周几

0x03 日历的开发

这里我们选择使用 collectionview,首先向storyboard中拖入一个collectionview,然后在viewcontroller中添加collectionview的协议

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] = []
      // 第一天之前的空白区域
      for number in 0..<firstdayofmonth-1 {
        daysarray.append("")
      }
      for number in firstdayofmonth-1...firstdayofmonth+numberofthemonth-2 {
        daysarray.append(string(number-firstdayofmonth+2))
      }
      // 最后一天后的空白区域
      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 uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网