新增日历的功能
点选日期,可以让区间内的Cell的颜色变成灰色
让操作可以更直觉
在didselectItemat内
使用
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
条件列出:
变数:
条件选择:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 作为点选的cell对象
selectItem = collectionView.cellForItem(at: indexPath)!
// 当前点选对象
var tap = indexPath.row
// 计算是否第一次参与 如果count = 1 那麽则是第一次参与
count += 1
if count == 1{
smalltap = tap
// 点选第一个cell变成灰色
selectItem.backgroundColor = UIColor.gray
}else
{
// 如果visited过了那麽就是已经有产生区间
if visted == false{
if tap < smalltap{
// 转换tap与smalltap
store = smalltap
smalltap = tap
tap = store
// 计算总共几天
day = tap - smalltap+1
// 让目前区间都变成灰色
for i in smalltap...tap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = UIColor.gray
}
count = 1
// 让目前最大变成当前触碰
bigtap = tap
// 让textfield的文字变成 总共几天累积
countDay.text = String(day)
}else
{
day = tap - smalltap+1
for i in smalltap...tap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = UIColor.gray
}
count = 1
bigtap = tap
countDay.text = String(day)
}
// 有区间产生讯号产生
visted = true
// 在有区间产生的状况下
}else
{
// 如果当前触碰是比目前最小还要小的时候
if tap < smalltap{
store = smalltap
smalltap = tap
tap = store
day = tap - smalltap+1
// 让 目前最小到当前触碰都变成灰色
for i in smalltap...tap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = UIColor.gray
}
// 让 当前触碰到目前最大颜色都变不见
for i in tap+1...bigtap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = .clear
}
bigtap = tap
countDay.text = String(day)
count = 1
// 如果当前触碰比目前最大还要大的时候
}else if (tap > bigtap)
{
store = bigtap
bigtap = tap
tap = store
day = bigtap - tap + 1
for i in tap...bigtap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = UIColor.gray
}
for i in smalltap...tap-1{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = .clear
}
smalltap = tap
countDay.text = String(day)
count = 1
// 如果当前触碰介於目前最大跟目前最小之间
}else if (smalltap < tap) && (tap < bigtap)
{
day = tap - smalltap + 1
// 让目前最小到当前触碰的区间变成灰色
for i in smalltap...tap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = UIColor.gray
}
// 让当前触碰到目前最大的区间的颜色变不见
for i in tap+1...bigtap{
selectItem = collectionView.cellForItem(at: [0,i])!
selectItem.backgroundColor = .clear
}
countDay.text = String(day)
bigtap = tap
count = 1
}
}
}
}
成果展示:
<<: Day 28 Compose UI ModalBottomSheetLayout
TCSEC定义了评估可信计算系统的标准,该系统包括四个分类。每个分类可以分为几类。B分类的类别(B1...
Portals 是一种让 children 可以 render 到 parent component...
串流处理的应用场景近年来越来越常见,尤其是IOT产业的蓬勃发展,加上硬体技术的发展,越来越多的即时资...
回顾 30天咻的一下就过去了,第一次的铁人赛暂时画下小句点,那些期待补的篇幅我没忘(?) 漂向何处 ...
今天就来看看JAVA中的运算子和运算式吧! 这边就先简单介绍一下谁是运算元谁是运算子吧~ int b...