Skip to content

Commit

Permalink
Redesign visible time(y offset) logic when weekview first time appears
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Zhang committed Sep 24, 2018
1 parent 18f6b30 commit cbc24b7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,31 @@ class CustomViewController: UIViewController {
private func setupCalendarView() {
calendarWeekView.baseDelegate = self

// For example only
if viewModel.currentSelectedData != nil {
// For example only
setupCalendarViewWithSelectedData()
return
} else {
// Normal setup with different visible time
let time8AM = DateComponents(calendar: Calendar.current, timeZone: Calendar.current.timeZone, year: 2018, month: 9, day: 24, hour: 8, minute: 0, second: 0).date!
calendarWeekView.setupCalendar(numOfDays: 3,
setDate: Date(),
allEvents: viewModel.eventsByDate,
scrollType: .pageScroll,
visibleTime: time8AM)
}

calendarWeekView.setupCalendar(numOfDays: 3,
setDate: Date(),
allEvents: viewModel.eventsByDate,
scrollType: .pageScroll)
}

/// For example only
private func setupCalendarViewWithSelectedData() {
guard let selectedData = viewModel.currentSelectedData else { return }

let time8AM = DateComponents(calendar: Calendar.current, timeZone: Calendar.current.timeZone, year: 2018, month: 9, day: 24, hour: 8, minute: 0, second: 0).date!
calendarWeekView.setupCalendar(numOfDays: selectedData.numOfDays,
setDate: selectedData.date,
allEvents: viewModel.eventsByDate,
scrollType: selectedData.scrollType,
firstDayOfWeek: selectedData.firstDayOfWeek)
firstDayOfWeek: selectedData.firstDayOfWeek,
visibleTime: time8AM)
calendarWeekView.updateFlowLayout(JZWeekViewFlowLayout(hourGridDivision: selectedData.hourGridDivision))
}

Expand Down
4 changes: 2 additions & 2 deletions Example/JZCalendarWeekViewExample/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.5.2</string>
<string>0.5.3</string>
<key>CFBundleVersion</key>
<string>18091701</string>
<string>18092401</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
2 changes: 1 addition & 1 deletion JZCalendarWeekView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "JZCalendarWeekView"
s.version = "0.5.2"
s.version = "0.5.3"
s.summary = "Calendar Week & Day View in iOS Swift"
s.homepage = "https://github.com/zjfjack/JZCalendarWeekView"
s.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
2 changes: 1 addition & 1 deletion JZCalendarWeekView/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.5.2</string>
<string>0.5.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
10 changes: 6 additions & 4 deletions JZCalendarWeekView/JZBaseWeekView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,21 @@ open class JZBaseWeekView: UIView {
Basic Setup method for JZCalendarWeekView,it **must** be called.

- Parameters:
- numOfDays: number of days in a page
- setDate: the initial set date, the first date in current page except WeekView (numOfDays = 7)
- numOfDays: Number of days in a page
- setDate: The initial set date, the first date in current page except WeekView (numOfDays = 7)
- allEvents: The dictionary of all the events for present. JZWeekViewHelper.getIntraEventsByDate can help transform the data
- firstDayOfWeek: First day of a week, **only works when numOfDays is 7**. Default value is Sunday
- scrollType: The horizontal scroll type for this view. Default value is pageScroll
- currentTimelineType: The current time line type for this view. Default value is section
- visibleTime: WeekView will be scroll to this time, when it appears the **first time**. This visibleTime only determines **y** offset. Defaut value is current time.
*/
open func setupCalendar(numOfDays: Int,
setDate: Date,
allEvents: [Date:[JZBaseEvent]],
scrollType: JZScrollType = .pageScroll,
firstDayOfWeek :DayOfWeek? = nil,
currentTimelineType: JZCurrentTimelineType = .section) {
currentTimelineType: JZCurrentTimelineType = .section,
visibleTime: Date = Date()) {

self.numOfDays = numOfDays
if numOfDays == 7 {
Expand All @@ -146,7 +148,7 @@ open class JZBaseWeekView: UIView {

if self.isFirstAppear {
self.isFirstAppear = false
self.flowLayout.scrollCollectionViewToCurrentTime()
self.flowLayout.scrollCollectionViewTo(time: visibleTime)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions JZCalendarWeekView/JZWeekViewFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,10 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout {
}
}

open func scrollCollectionViewToCurrentTime() {
let y = max(0, min(CGFloat(Calendar.current.component(.hour, from: Date())) * hourHeight - collectionView!.frame.height / 2 + columnHeaderHeight,
/// Vertically scroll the collectionView to specific time in a day, only **hour** will be calulated for the offset.
/// If the hour you set is too large, it will only reach the bottom 24:00 as the maximum value.
open func scrollCollectionViewTo(time: Date) {
let y = max(0, min(CGFloat(Calendar.current.component(.hour, from: time)) * hourHeight,
collectionView!.contentSize.height - collectionView!.frame.height))

self.collectionView!.setContentOffsetWithoutDelegate(CGPoint(x: self.collectionView!.contentOffset.x, y: y), animated: false)
Expand Down

0 comments on commit cbc24b7

Please sign in to comment.