Skip to content

Commit

Permalink
Merge pull request #37 from zjfjack/bugfix-allDayScrollFromBottom
Browse files Browse the repository at this point in the history
Bugfix all day scroll from bottom
  • Loading branch information
zjfjack authored Dec 12, 2018
2 parents 62c772b + dc57db0 commit eb789aa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
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.5</string>
<string>0.5.6</string>
<key>CFBundleVersion</key>
<string>18110801</string>
<string>18121201</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.5"
s.version = "0.5.6"
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.5</string>
<string>0.5.6</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
36 changes: 28 additions & 8 deletions JZCalendarWeekView/JZBaseWeekView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ open class JZBaseWeekView: UIView {
maxEventsCount = count
}
}
flowLayout.allDayHeaderHeight = flowLayout.defaultAllDayOneLineHeight * CGFloat(min(maxEventsCount, 2))
let newAllDayHeader = flowLayout.defaultAllDayOneLineHeight * CGFloat(min(maxEventsCount, 2))
if newAllDayHeader != flowLayout.allDayHeaderHeight {
// Check whether we need update the allDayHeaderHeight
if !isScrolling || !willEffectContentSize(difference: flowLayout.allDayHeaderHeight - newAllDayHeader) {
flowLayout.allDayHeaderHeight = newAllDayHeader
}
}
}

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

// initial day is one page before the settle day
collectionView.setContentOffsetWithoutDelegate(CGPoint(x:contentViewWidth, y:collectionView.contentOffset.y), animated: false)
updateAllDayBar(isScrolling: false)

// initial day is one page before the settle day
collectionView.setContentOffsetWithoutDelegate(CGPoint(x:contentViewWidth, y:getYOffset()), animated: false)
flowLayout.invalidateLayoutCache()
collectionView.reloadData()
}


/// Notice: A temporary solution to fix the scroll from bottom issue when isScrolling
/// The issue is because the decreased height value will cause the system to change the collectionView contentOffset, but the affected contentOffset will
/// 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
private func willEffectContentSize(difference: CGFloat) -> Bool {
return collectionView.contentOffset.y + difference + collectionView.bounds.height > collectionView.contentSize.height
}

/// Fix collectionView scroll from bottom (contentsize height decreased) wrong offset issue
private func getYOffset() -> CGFloat {
guard isAllDaySupported else { return collectionView.contentOffset.y }
let bottomOffset = flowLayout.collectionViewContentSize.height - collectionView.bounds.height
if collectionView.contentOffset.y > bottomOffset {
return bottomOffset
} else {
return collectionView.contentOffset.y
}
}

/// Reload the WeekView to date with no animation
/// - Parameters:
Expand Down
24 changes: 13 additions & 11 deletions JZCalendarWeekView/JZWeekViewFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout {
}

open func prepareHorizontalTileSectionLayoutForSections(_ sectionIndexes: NSIndexSet) {
guard collectionView!.numberOfSections != 0 else { return }
guard let collectionView = collectionView, collectionView.numberOfSections != 0 else { return }

var attributes = UICollectionViewLayoutAttributes()

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

// Row header
let rowHeaderMinX = fmax(collectionView!.contentOffset.x, 0)
let rowHeaderMinX = fmax(collectionView.contentOffset.x, 0)

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

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

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

(attributes, allDayCornerAttributes) =
layoutAttributesForDecorationView(at: IndexPath(item: 0, section: 0),
ofKind: JZDecorationViewKinds.allDayCorner,
withItemCache: allDayCornerAttributes)
attributes.frame = CGRect(origin: CGPoint(x:collectionView!.contentOffset.x,y: collectionView!.contentOffset.y + columnHeaderHeight),
attributes.frame = CGRect(origin: CGPoint(x:collectionView.contentOffset.x,y: collectionView.contentOffset.y + columnHeaderHeight),
size: CGSize(width: rowHeaderWidth, height: allDayHeaderHeight))
attributes.zIndex = zIndexForElementKind(JZDecorationViewKinds.allDayCorner)

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


// Column Header
let columnHeaderMinY = fmax(collectionView!.contentOffset.y, 0.0)
let columnHeaderMinY = fmax(collectionView.contentOffset.y, 0.0)
print(columnHeaderMinY, collectionView.contentSize.height)

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

0 comments on commit eb789aa

Please sign in to comment.