From 026daf6d6970766487eedb27edd3ffafb3d470c0 Mon Sep 17 00:00:00 2001 From: Jeff Zhang Date: Tue, 11 Dec 2018 17:34:48 +1100 Subject: [PATCH 1/2] Fixed quick swipe gesture issue, exclude the slow swipe flickering issue --- JZCalendarWeekView/JZBaseWeekView.swift | 28 +++++++++++++------ JZCalendarWeekView/JZWeekViewFlowLayout.swift | 24 ++++++++-------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/JZCalendarWeekView/JZBaseWeekView.swift b/JZCalendarWeekView/JZBaseWeekView.swift index 910fccf..2c6761c 100644 --- a/JZCalendarWeekView/JZBaseWeekView.swift +++ b/JZCalendarWeekView/JZBaseWeekView.swift @@ -186,7 +186,7 @@ open class JZBaseWeekView: UIView { } open func updateAllDayBar(isScrolling: Bool) { - guard isAllDaySupported else { return } + guard isAllDaySupported && !isScrolling else { return } var maxEventsCount: Int = 0 getDatesInCurrentPage(isScrolling: isScrolling).forEach { let count = allDayEventsBySection[$0]?.count ?? 0 @@ -194,7 +194,10 @@ 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 { + 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 @@ -212,18 +215,25 @@ 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() } - + + /// 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: diff --git a/JZCalendarWeekView/JZWeekViewFlowLayout.swift b/JZCalendarWeekView/JZWeekViewFlowLayout.swift index 91e738a..0ab8207 100644 --- a/JZCalendarWeekView/JZWeekViewFlowLayout.swift +++ b/JZCalendarWeekView/JZWeekViewFlowLayout.swift @@ -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() @@ -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), @@ -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) @@ -255,8 +256,8 @@ 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) @@ -264,7 +265,7 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout { 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) @@ -272,13 +273,14 @@ open class JZWeekViewFlowLayout: UICollectionViewFlowLayout { (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) From dc57db06a80acfbcad441468265169a448828e27 Mon Sep 17 00:00:00 2001 From: Jeff Zhang Date: Wed, 12 Dec 2018 11:52:09 +1100 Subject: [PATCH 2/2] Fix swipe at bottom issue by temporarily disabling AllDayHeight change --- .../Supporting Files/Info.plist | 4 ++-- JZCalendarWeekView.podspec | 2 +- JZCalendarWeekView/Info.plist | 2 +- JZCalendarWeekView/JZBaseWeekView.swift | 14 ++++++++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Example/JZCalendarWeekViewExample/Supporting Files/Info.plist b/Example/JZCalendarWeekViewExample/Supporting Files/Info.plist index 025ea6a..82fa3c1 100644 --- a/Example/JZCalendarWeekViewExample/Supporting Files/Info.plist +++ b/Example/JZCalendarWeekViewExample/Supporting Files/Info.plist @@ -15,9 +15,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.5.5 + 0.5.6 CFBundleVersion - 18110801 + 18121201 LSRequiresIPhoneOS UILaunchStoryboardName diff --git a/JZCalendarWeekView.podspec b/JZCalendarWeekView.podspec index c09672f..b29eb8a 100644 --- a/JZCalendarWeekView.podspec +++ b/JZCalendarWeekView.podspec @@ -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" } diff --git a/JZCalendarWeekView/Info.plist b/JZCalendarWeekView/Info.plist index 738f135..b55c527 100644 --- a/JZCalendarWeekView/Info.plist +++ b/JZCalendarWeekView/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.5.5 + 0.5.6 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/JZCalendarWeekView/JZBaseWeekView.swift b/JZCalendarWeekView/JZBaseWeekView.swift index 2c6761c..a09c161 100644 --- a/JZCalendarWeekView/JZBaseWeekView.swift +++ b/JZCalendarWeekView/JZBaseWeekView.swift @@ -186,7 +186,7 @@ open class JZBaseWeekView: UIView { } open func updateAllDayBar(isScrolling: Bool) { - guard isAllDaySupported && !isScrolling else { return } + guard isAllDaySupported else { return } var maxEventsCount: Int = 0 getDatesInCurrentPage(isScrolling: isScrolling).forEach { let count = allDayEventsBySection[$0]?.count ?? 0 @@ -196,7 +196,10 @@ open class JZBaseWeekView: UIView { } let newAllDayHeader = flowLayout.defaultAllDayOneLineHeight * CGFloat(min(maxEventsCount, 2)) if newAllDayHeader != flowLayout.allDayHeaderHeight { - flowLayout.allDayHeaderHeight = newAllDayHeader + // Check whether we need update the allDayHeaderHeight + if !isScrolling || !willEffectContentSize(difference: flowLayout.allDayHeaderHeight - newAllDayHeader) { + flowLayout.allDayHeaderHeight = newAllDayHeader + } } } @@ -224,6 +227,13 @@ open class JZBaseWeekView: UIView { 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 }