From e09eb21e1f75e2fe20ebec1fbe57791ef5e309b7 Mon Sep 17 00:00:00 2001 From: Eliah Snakin Date: Thu, 16 Nov 2017 18:33:45 +0300 Subject: [PATCH 1/5] Swift 4 --- Source/HFCardCollectionViewLayout.swift | 85 ++++++++----------------- 1 file changed, 26 insertions(+), 59 deletions(-) diff --git a/Source/HFCardCollectionViewLayout.swift b/Source/HFCardCollectionViewLayout.swift index 44bb4ba..e9eeac0 100644 --- a/Source/HFCardCollectionViewLayout.swift +++ b/Source/HFCardCollectionViewLayout.swift @@ -225,21 +225,18 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz /// Default: false @IBInspectable open var collapseAllCards: Bool = false { didSet { - self.flipRevealedCardBack(completion: { - self.collectionView?.isScrollEnabled = !self.collapseAllCards - var previousRevealedIndex = -1 - let collectionViewLayoutDelegate = self.collectionView?.delegate as? HFCardCollectionViewLayoutDelegate - if(self.revealedIndex >= 0) { - previousRevealedIndex = self.revealedIndex - collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, willUnrevealCardAtIndex: self.revealedIndex) - self.revealedIndex = -1 + self.collectionView?.isScrollEnabled = !self.collapseAllCards + var previousRevealedIndex = -1 + let collectionViewLayoutDelegate = self.collectionView?.delegate as? HFCardCollectionViewLayoutDelegate + if(self.revealedIndex >= 0) { + previousRevealedIndex = self.revealedIndex + collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, willUnrevealCardAtIndex: self.revealedIndex) + self.revealedIndex = -1 + } + self.collectionView?.performBatchUpdates({ self.collectionView?.reloadData() }, completion: {(finished) in + if(previousRevealedIndex >= 0) { + collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, didUnrevealCardAtIndex: previousRevealedIndex) } - self.collectionView?.performBatchUpdates({ self.collectionView?.reloadData() }, completion: {(finished) in - if(previousRevealedIndex >= 0) { - - collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, didUnrevealCardAtIndex: previousRevealedIndex) - } - }) }) } } @@ -318,14 +315,12 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, willUnrevealCardAtIndex: self.revealedIndex) } if index >= 0 { - self.revealedIndex = index if(collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, canRevealCardAtIndex: index) == false) { self.revealedIndex = -1 - self.collectionView?.isScrollEnabled = true - self.deinitializeRevealedCard() return } collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, willRevealCardAtIndex: index) + self.revealedIndex = index _ = self.initializeRevealedCard() self.collectionView?.isScrollEnabled = false @@ -335,7 +330,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz }) } else if(self.revealedIndex >= 0) { self.revealedIndex = index - self.collectionView?.isScrollEnabled = false + self.collectionView?.isScrollEnabled = true self.collectionView?.performBatchUpdates({ self.collectionView?.reloadData() }, completion: { (finished) in collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, didUnrevealCardAtIndex: oldRevealedIndex) completion?() @@ -421,38 +416,16 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz } } - private var TEMPTOP: CGFloat = 0 - open func willInsert(indexPaths: [IndexPath]) { for indexPath in indexPaths { if(indexPath.section == 0) { if(indexPath.item <= self.revealedIndex) { - TEMPTOP += self.cardHeadHeight self.revealedIndex += 1 } } } } - open func willDelete(indexPaths: [IndexPath]) { - let collectionViewLayoutDelegate = self.collectionView?.delegate as? HFCardCollectionViewLayoutDelegate - for indexPath in indexPaths { - if(indexPath.section == 0) { - if(indexPath.item == self.revealedIndex) { - self.revealedIndex = -1 - self.collectionView?.isScrollEnabled = true - self.deinitializeRevealedCard() - collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, willUnrevealCardAtIndex: indexPath.item) - //collectionViewLayoutDelegate?.cardCollectionViewLayout?(self, didUnrevealCardAtIndex: indexPath.item) - } - if(indexPath.item <= self.revealedIndex) { - TEMPTOP -= self.cardHeadHeight - self.revealedIndex -= 1 - } - } - } - } - //////////////////////////////////////////////////////////////////////////////////////////////////// ////////// Private ////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -497,11 +470,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz private var contentInset: UIEdgeInsets { get { - if #available(iOS 11, *) { - return self.collectionView!.adjustedContentInset - } else { - return self.collectionView!.contentInset - } + return self.collectionView!.contentInset } } @@ -549,11 +518,11 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz self.collectionView?.addGestureRecognizer(self.collectionViewTapGestureRecognizer!) } - func keyboardWillShow(_ notification: Notification) { + @objc func keyboardWillShow(_ notification: Notification) { self.collectionViewIgnoreBottomContentOffsetChanges = true } - func keyboardDidHide(_ notification: Notification) { + @objc func keyboardDidHide(_ notification: Notification) { self.collectionViewIgnoreBottomContentOffsetChanges = false } @@ -609,7 +578,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz /// - Parameter proposedContentOffset: The proposed point (in the collection view’s content view) at which to stop scrolling. This is the value at which scrolling would naturally stop if no adjustments were made. The point reflects the upper-left corner of the visible content. /// - Parameter velocity: The current scrolling velocity along both the horizontal and vertical axes. This value is measured in points per second. override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { - let proposedContentOffsetY = proposedContentOffset.y + self.contentInset.top + let proposedContentOffsetY = proposedContentOffset.y + self.collectionView!.contentInset.top if(self.spaceAtTopShouldSnap == true && self.spaceAtTopForBackgroundView > 0) { if(proposedContentOffsetY > 0 && proposedContentOffsetY < self.spaceAtTopForBackgroundView) { let scrollToTopY = self.spaceAtTopForBackgroundView * 0.5 @@ -673,7 +642,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz // MARK: Private Functions for UICollectionViewLayout - internal func collectionViewTapGestureHandler() { + @objc internal func collectionViewTapGestureHandler() { if let tapLocation = self.collectionViewTapGestureRecognizer?.location(in: self.collectionView) { if let indexPath = self.collectionView?.indexPathForItem(at: tapLocation) { self.collectionView?.delegate?.collectionView?(self.collectionView!, didSelectItemAt: indexPath) @@ -698,8 +667,8 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz shouldReloadAllItems = true } - var startIndex = Int((self.collectionView!.contentOffset.y + self.contentInset.top - self.spaceAtTopForBackgroundView + TEMPTOP) / self.cardHeadHeight) - 10 - var endBeforeIndex = Int((self.collectionView!.contentOffset.y + self.collectionView!.frame.size.height + TEMPTOP) / self.cardHeadHeight) + 5 + var startIndex = Int((self.collectionView!.contentOffset.y + self.contentInset.top - self.spaceAtTopForBackgroundView) / self.cardHeadHeight) - 10 + var endBeforeIndex = Int((self.collectionView!.contentOffset.y + self.collectionView!.frame.size.height) / self.cardHeadHeight) + 5 if(startIndex < 0) { startIndex = 0 @@ -721,8 +690,6 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz cardLayoutAttribute.zIndex = itemIndex if self.revealedIndex < 0 && self.collapseAllCards == false { - self.collectionView!.contentOffset.y += TEMPTOP - TEMPTOP = 0 self.generateNonRevealedCardsAttribute(cardLayoutAttribute) } else if self.revealedIndex == itemIndex && self.collapseAllCards == false { self.generateRevealedCardAttribute(cardLayoutAttribute) @@ -744,7 +711,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz if self.collapseAllCards == false { return [] } else { - let startIndex: Int = Int(self.contentOffsetTop + TEMPTOP / self.cardHeadHeight) + let startIndex: Int = Int(self.contentOffsetTop / self.cardHeadHeight) let endIndex = max(0, startIndex + self.bottomNumberOfStackedCards - 2) return Array(startIndex...endIndex) } @@ -778,7 +745,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz private func generateNonRevealedCardsAttribute(_ attribute: HFCardCollectionViewLayoutAttributes) { let cardHeadHeight = self.calculateCardHeadHeight() - let startIndex = Int((self.contentOffsetTop + TEMPTOP - self.spaceAtTopForBackgroundView) / cardHeadHeight) + let startIndex = Int((self.contentOffsetTop - self.spaceAtTopForBackgroundView) / cardHeadHeight) let currentIndex = attribute.indexPath.item if(currentIndex == self.movingCardSelectedIndex) { attribute.alpha = 0.0 @@ -893,7 +860,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz } } - internal func revealedCardPanGestureHandler() { + @objc internal func revealedCardPanGestureHandler() { if self.collectionViewItemCount == 1 || self.revealedCardIsFlipped == true { return } @@ -933,7 +900,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz // MARK: Moving Card - internal func movingCardGestureHandler() { + @objc internal func movingCardGestureHandler() { let moveUpOffset: CGFloat = 20 if let movingCardGestureRecognizer = self.movingCardGestureRecognizer { @@ -1088,7 +1055,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz self.autoscrollDisplayLink = nil } - internal func autoscrollHandler(displayLink: CADisplayLink) { + @objc internal func autoscrollHandler(displayLink: CADisplayLink) { let direction = self.autoscrollDirection if(direction == .unknown) { return @@ -1098,7 +1065,7 @@ open class HFCardCollectionViewLayout: UICollectionViewLayout, UIGestureRecogniz let frameSize = self.collectionView!.frame.size let contentSize = self.collectionView!.contentSize let contentOffset = self.collectionView!.contentOffset - let contentInset = self.contentInset + let contentInset = self.collectionView!.contentInset var distance: CGFloat = CGFloat(rint(scrollMultiplier * displayLink.duration)) var translation = CGPoint.zero From cffdc01ce07f2f736616510374c360dea080bd35 Mon Sep 17 00:00:00 2001 From: Eliah Snakin Date: Thu, 16 Nov 2017 18:41:51 +0300 Subject: [PATCH 2/5] deleted seem not to work --- Source/HFCardCollectionView.swift | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Source/HFCardCollectionView.swift b/Source/HFCardCollectionView.swift index 41e3e5c..5a655df 100644 --- a/Source/HFCardCollectionView.swift +++ b/Source/HFCardCollectionView.swift @@ -16,12 +16,5 @@ class HFCardCollectionView: UICollectionView { } super.insertItems(at: indexPaths) } - - override func deleteItems(at indexPaths: [IndexPath]) { - if let collectionViewLayout = self.collectionViewLayout as? HFCardCollectionViewLayout { - collectionViewLayout.willDelete(indexPaths: indexPaths) - } - super.deleteItems(at: indexPaths) - } - + } From f64a5c952a52e4190ea2632d81c65a3bef9eb9c4 Mon Sep 17 00:00:00 2001 From: Eliah Snakin Date: Thu, 16 Nov 2017 18:57:27 +0300 Subject: [PATCH 3/5] HFCardCollectionView added to project --- HFCardCollectionViewLayout.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/HFCardCollectionViewLayout.xcodeproj/project.pbxproj b/HFCardCollectionViewLayout.xcodeproj/project.pbxproj index 19f5509..885b435 100644 --- a/HFCardCollectionViewLayout.xcodeproj/project.pbxproj +++ b/HFCardCollectionViewLayout.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 416D2D721E5F117500D8A570 /* HFCardCollectionViewLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 416D2D6E1E5F117500D8A570 /* HFCardCollectionViewLayout.swift */; }; 416D2D731E5F117500D8A570 /* HFCardCollectionViewLayoutDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 416D2D6F1E5F117500D8A570 /* HFCardCollectionViewLayoutDelegate.swift */; }; 416D2D741E5F117500D8A570 /* UICollectionViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 416D2D701E5F117500D8A570 /* UICollectionViewExtensions.swift */; }; + D0F827051FBDECAF00A0902D /* HFCardCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0F827041FBDECAF00A0902D /* HFCardCollectionView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -22,6 +23,7 @@ 416D2D6E1E5F117500D8A570 /* HFCardCollectionViewLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = HFCardCollectionViewLayout.swift; path = Source/HFCardCollectionViewLayout.swift; sourceTree = SOURCE_ROOT; }; 416D2D6F1E5F117500D8A570 /* HFCardCollectionViewLayoutDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = HFCardCollectionViewLayoutDelegate.swift; path = Source/HFCardCollectionViewLayoutDelegate.swift; sourceTree = SOURCE_ROOT; }; 416D2D701E5F117500D8A570 /* UICollectionViewExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UICollectionViewExtensions.swift; path = Source/UICollectionViewExtensions.swift; sourceTree = SOURCE_ROOT; }; + D0F827041FBDECAF00A0902D /* HFCardCollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = HFCardCollectionView.swift; path = Source/HFCardCollectionView.swift; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -54,6 +56,7 @@ 416D2D641E5F113600D8A570 /* HFCardCollectionViewLayout */ = { isa = PBXGroup; children = ( + D0F827041FBDECAF00A0902D /* HFCardCollectionView.swift */, 416D2D6D1E5F117500D8A570 /* HFCardCollectionViewCell.swift */, 416D2D6E1E5F117500D8A570 /* HFCardCollectionViewLayout.swift */, 416D2D6F1E5F117500D8A570 /* HFCardCollectionViewLayoutDelegate.swift */, @@ -145,6 +148,7 @@ buildActionMask = 2147483647; files = ( 416D2D741E5F117500D8A570 /* UICollectionViewExtensions.swift in Sources */, + D0F827051FBDECAF00A0902D /* HFCardCollectionView.swift in Sources */, 416D2D721E5F117500D8A570 /* HFCardCollectionViewLayout.swift in Sources */, 416D2D711E5F117500D8A570 /* HFCardCollectionViewCell.swift in Sources */, 416D2D731E5F117500D8A570 /* HFCardCollectionViewLayoutDelegate.swift in Sources */, From 34ca1c5b6fd1d9429e280372a71009f685e29b0b Mon Sep 17 00:00:00 2001 From: Eliah Snakin Date: Thu, 16 Nov 2017 19:05:12 +0300 Subject: [PATCH 4/5] opening HFCardCollectionView --- Source/HFCardCollectionView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/HFCardCollectionView.swift b/Source/HFCardCollectionView.swift index 5a655df..1ac2548 100644 --- a/Source/HFCardCollectionView.swift +++ b/Source/HFCardCollectionView.swift @@ -8,7 +8,7 @@ import UIKit -class HFCardCollectionView: UICollectionView { +open class HFCardCollectionView: UICollectionView { override func insertItems(at indexPaths: [IndexPath]) { if let collectionViewLayout = self.collectionViewLayout as? HFCardCollectionViewLayout { From 2b1a3f2023cda4fab5b931c98f79c7d088c30d6e Mon Sep 17 00:00:00 2001 From: Eliah Snakin Date: Thu, 16 Nov 2017 19:12:15 +0300 Subject: [PATCH 5/5] opening HFCardCollectionView --- Source/HFCardCollectionView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/HFCardCollectionView.swift b/Source/HFCardCollectionView.swift index 1ac2548..bd901c5 100644 --- a/Source/HFCardCollectionView.swift +++ b/Source/HFCardCollectionView.swift @@ -10,7 +10,7 @@ import UIKit open class HFCardCollectionView: UICollectionView { - override func insertItems(at indexPaths: [IndexPath]) { + override open func insertItems(at indexPaths: [IndexPath]) { if let collectionViewLayout = self.collectionViewLayout as? HFCardCollectionViewLayout { collectionViewLayout.willInsert(indexPaths: indexPaths) }