我想在网上找到的快速写的Karmadust日历中添加一些东西:
http://blog.karmadust.com/lets-create-a-calendar-using-a-uicollectionview/#comment-941
我想要做的是在加载时显示加载视图的当前部分。
我有点挣扎。
我发现,感谢stackoverflow,我想实现的方法.scrollToItemAtIndexPath。
日历的内容:
有几个文件。其中一个是KDCalendarView:UIView。 有一些计算,但我想使用的主要是todayIndexPath:NSIndexPath - 这是今天日期的项目的位置。
我再次在功能中再次在ViewController中实现todayIndexPath的代码。
以下代码:
func getTodayIndexPath() -> NSIndexPath {
let startDateCache = startDate()
let endDateCache = endDate()
var startOfMonthCache = NSDate()
var todayIndexPath: NSIndexPath?
let dayOneComponents = NSCalendar.currentCalendar().components( [NSCalendarUnit.Era, NSCalendarUnit.Year, NSCalendarUnit.Month], fromDate: startDateCache!)
if let gmtCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian),
gmtTimeZone = NSTimeZone(abbreviation: "GMT") {
gmtCalendar.timeZone = gmtTimeZone
if let dateFromDayOneComponents = gmtCalendar.dateFromComponents(dayOneComponents) {
startOfMonthCache = dateFromDayOneComponents
let today = NSDate()
if startOfMonthCache.compare(today) == NSComparisonResult.OrderedAscending &&
endDateCache!.compare(today) == NSComparisonResult.OrderedDescending {
let differenceFromTodayComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Month, NSCalendarUnit.Day], fromDate: startOfMonthCache, toDate: NSDate(), options: NSCalendarOptions())
todayIndexPath = NSIndexPath(forItem: differenceFromTodayComponents.day, inSection: differenceFromTodayComponents.month)
}
}
}
return todayIndexPath!
}
在viewDidAppear中我添加了:
let index: NSIndexPath = getTodayIndexPath()
self.calendarView.calendarView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
一切似乎都很好,我得到了理想的观点,但是: