Skip to content

Commit eb789aa

Browse files
authored
Merge pull request #37 from zjfjack/bugfix-allDayScrollFromBottom
Bugfix all day scroll from bottom
2 parents 62c772b + dc57db0 commit eb789aa

5 files changed

Lines changed: 45 additions & 23 deletions

File tree

Example/JZCalendarWeekViewExample/Supporting Files/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.5.5</string>
18+
<string>0.5.6</string>
1919
<key>CFBundleVersion</key>
20-
<string>18110801</string>
20+
<string>18121201</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>UILaunchStoryboardName</key>

JZCalendarWeekView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "JZCalendarWeekView"
3-
s.version = "0.5.5"
3+
s.version = "0.5.6"
44
s.summary = "Calendar Week & Day View in iOS Swift"
55
s.homepage = "https://github.com/zjfjack/JZCalendarWeekView"
66
s.license = { :type => "MIT", :file => "LICENSE" }

JZCalendarWeekView/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.5.5</string>
18+
<string>0.5.6</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSPrincipalClass</key>

JZCalendarWeekView/JZBaseWeekView.swift

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ open class JZBaseWeekView: UIView {
194194
maxEventsCount = count
195195
}
196196
}
197-
flowLayout.allDayHeaderHeight = flowLayout.defaultAllDayOneLineHeight * CGFloat(min(maxEventsCount, 2))
197+
let newAllDayHeader = flowLayout.defaultAllDayOneLineHeight * CGFloat(min(maxEventsCount, 2))
198+
if newAllDayHeader != flowLayout.allDayHeaderHeight {
199+
// Check whether we need update the allDayHeaderHeight
200+
if !isScrolling || !willEffectContentSize(difference: flowLayout.allDayHeaderHeight - newAllDayHeader) {
201+
flowLayout.allDayHeaderHeight = newAllDayHeader
202+
}
203+
}
198204
}
199205

200206
/// Update collectionViewLayout with custom flowLayout. For some other values like gridThickness and contentsMargin, please inherit from JZWeekViewFlowLayout to change the default value
@@ -212,18 +218,32 @@ open class JZBaseWeekView: UIView {
212218
/// - Parameters:
213219
/// - reloadEvents: If provided new events, current events will be reloaded. Default value is nil.
214220
open func forceReload(reloadEvents: [Date: [JZBaseEvent]]? = nil) {
215-
if let events = reloadEvents {
216-
self.allEventsBySection = events
217-
}
221+
if let events = reloadEvents { self.allEventsBySection = events }
218222

219-
// initial day is one page before the settle day
220-
collectionView.setContentOffsetWithoutDelegate(CGPoint(x:contentViewWidth, y:collectionView.contentOffset.y), animated: false)
221223
updateAllDayBar(isScrolling: false)
222-
224+
// initial day is one page before the settle day
225+
collectionView.setContentOffsetWithoutDelegate(CGPoint(x:contentViewWidth, y:getYOffset()), animated: false)
223226
flowLayout.invalidateLayoutCache()
224227
collectionView.reloadData()
225228
}
226-
229+
230+
/// Notice: A temporary solution to fix the scroll from bottom issue when isScrolling
231+
/// The issue is because the decreased height value will cause the system to change the collectionView contentOffset, but the affected contentOffset will
232+
/// greater than the contentSize height, and the view will show some abnormal updates, this value will be used with isScrolling to check whether the in scroling change will be applied
233+
private func willEffectContentSize(difference: CGFloat) -> Bool {
234+
return collectionView.contentOffset.y + difference + collectionView.bounds.height > collectionView.contentSize.height
235+
}
236+
237+
/// Fix collectionView scroll from bottom (contentsize height decreased) wrong offset issue
238+
private func getYOffset() -> CGFloat {
239+
guard isAllDaySupported else { return collectionView.contentOffset.y }
240+
let bottomOffset = flowLayout.collectionViewContentSize.height - collectionView.bounds.height
241+
if collectionView.contentOffset.y > bottomOffset {
242+
return bottomOffset
243+
} else {
244+
return collectionView.contentOffset.y
245+
}
246+
}
227247

228248
/// Reload the WeekView to date with no animation
229249
/// - Parameters:

JZCalendarWeekView/JZWeekViewFlowLayout.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout {
187187
}
188188

189189
open func prepareHorizontalTileSectionLayoutForSections(_ sectionIndexes: NSIndexSet) {
190-
guard collectionView!.numberOfSections != 0 else { return }
190+
guard let collectionView = collectionView, collectionView.numberOfSections != 0 else { return }
191+
191192
var attributes = UICollectionViewLayoutAttributes()
192193

193194
let sectionHeight = (hourHeight * 24).toDecimal1Value()
@@ -213,11 +214,11 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout {
213214
(attributes, cornerHeaderAttributes) = layoutAttributesForSupplemantaryView(at: IndexPath(item: 0, section: 0),
214215
ofKind: JZSupplementaryViewKinds.cornerHeader,
215216
withItemCache: cornerHeaderAttributes)
216-
attributes.frame = CGRect(origin: collectionView!.contentOffset, size: CGSize(width: rowHeaderWidth, height: columnHeaderHeight))
217+
attributes.frame = CGRect(origin: collectionView.contentOffset, size: CGSize(width: rowHeaderWidth, height: columnHeaderHeight))
217218
attributes.zIndex = zIndexForElementKind(JZSupplementaryViewKinds.cornerHeader)
218219

219220
// Row header
220-
let rowHeaderMinX = fmax(collectionView!.contentOffset.x, 0)
221+
let rowHeaderMinX = fmax(collectionView.contentOffset.x, 0)
221222

222223
for rowHeaderIndex in 0...24 {
223224
(attributes, rowHeaderAttributes) = layoutAttributesForSupplemantaryView(at: IndexPath(item: rowHeaderIndex, section: 0),
@@ -232,11 +233,11 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout {
232233
(attributes, rowHeaderBackgroundAttributes) = layoutAttributesForDecorationView(at: IndexPath(item: 0, section: 0),
233234
ofKind: JZDecorationViewKinds.rowHeaderBackground,
234235
withItemCache: rowHeaderBackgroundAttributes)
235-
attributes.frame = CGRect(x: rowHeaderMinX, y: collectionView!.contentOffset.y, width: rowHeaderWidth, height: collectionView!.frame.height)
236+
attributes.frame = CGRect(x: rowHeaderMinX, y: collectionView.contentOffset.y, width: rowHeaderWidth, height: collectionView.frame.height)
236237
attributes.zIndex = zIndexForElementKind(JZDecorationViewKinds.rowHeaderBackground)
237238

238239
// All-Day header
239-
let allDayHeaderMinY = fmax(collectionView!.contentOffset.y + columnHeaderHeight, columnHeaderHeight)
240+
let allDayHeaderMinY = fmax(collectionView.contentOffset.y + columnHeaderHeight, columnHeaderHeight)
240241

241242
sectionIndexes.enumerate(_:) { (section, stop) in
242243
let sectionMinX = calendarContentMinX + sectionWidth * CGFloat(section)
@@ -255,30 +256,31 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout {
255256
layoutAttributesForDecorationView(at: IndexPath(item: 0, section: 0),
256257
ofKind: JZDecorationViewKinds.allDayHeaderBackground,
257258
withItemCache: allDayHeaderBackgroundAttributes)
258-
attributes.frame = CGRect(origin: CGPoint(x:collectionView!.contentOffset.x,y: collectionView!.contentOffset.y + columnHeaderHeight) ,
259-
size: CGSize(width: collectionView!.frame.width,
259+
attributes.frame = CGRect(origin: CGPoint(x:collectionView.contentOffset.x,y: collectionView.contentOffset.y + columnHeaderHeight) ,
260+
size: CGSize(width: collectionView.frame.width,
260261
height: allDayHeaderHeight))
261262
attributes.zIndex = zIndexForElementKind(JZDecorationViewKinds.allDayHeaderBackground)
262263

263264
(attributes, allDayCornerAttributes) =
264265
layoutAttributesForDecorationView(at: IndexPath(item: 0, section: 0),
265266
ofKind: JZDecorationViewKinds.allDayCorner,
266267
withItemCache: allDayCornerAttributes)
267-
attributes.frame = CGRect(origin: CGPoint(x:collectionView!.contentOffset.x,y: collectionView!.contentOffset.y + columnHeaderHeight),
268+
attributes.frame = CGRect(origin: CGPoint(x:collectionView.contentOffset.x,y: collectionView.contentOffset.y + columnHeaderHeight),
268269
size: CGSize(width: rowHeaderWidth, height: allDayHeaderHeight))
269270
attributes.zIndex = zIndexForElementKind(JZDecorationViewKinds.allDayCorner)
270271

271272
// column header background
272273
(attributes, columnHeaderBackgroundAttributes) = layoutAttributesForDecorationView(at: IndexPath(item: 0, section: 0),
273274
ofKind: JZDecorationViewKinds.columnHeaderBackground,
274275
withItemCache: columnHeaderBackgroundAttributes)
275-
let attributesHeight = columnHeaderHeight + (collectionView!.contentOffset.y < 0 ? abs(collectionView!.contentOffset.y) : 0 )
276-
attributes.frame = CGRect(origin: collectionView!.contentOffset, size: CGSize(width: collectionView!.frame.width, height: attributesHeight))
276+
let attributesHeight = columnHeaderHeight + (collectionView.contentOffset.y < 0 ? abs(collectionView.contentOffset.y) : 0 )
277+
attributes.frame = CGRect(origin: collectionView.contentOffset, size: CGSize(width: collectionView.frame.width, height: attributesHeight))
277278
attributes.zIndex = zIndexForElementKind(JZDecorationViewKinds.columnHeaderBackground)
278279

279280

280281
// Column Header
281-
let columnHeaderMinY = fmax(collectionView!.contentOffset.y, 0.0)
282+
let columnHeaderMinY = fmax(collectionView.contentOffset.y, 0.0)
283+
print(columnHeaderMinY, collectionView.contentSize.height)
282284

283285
sectionIndexes.enumerate(_:) { (section, stop) in
284286
let sectionMinX = calendarContentMinX + sectionWidth * CGFloat(section)

0 commit comments

Comments
 (0)