From 9e22d5be83be5830c5f12a24bfc03b1c47896b2e Mon Sep 17 00:00:00 2001 From: rsbittorf Date: Wed, 29 Jan 2020 11:17:43 -0800 Subject: [PATCH] Add move to page functionality --- .../Classes/Card Parts/CardPartPagedView.swift | 17 +++++++++++++++++ .../CardPartPagedViewCardController.swift | 17 ++++++++++++++--- README.md | 6 ++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CardParts/src/Classes/Card Parts/CardPartPagedView.swift b/CardParts/src/Classes/Card Parts/CardPartPagedView.swift index 3a6e6640..54b6e53c 100644 --- a/CardParts/src/Classes/Card Parts/CardPartPagedView.swift +++ b/CardParts/src/Classes/Card Parts/CardPartPagedView.swift @@ -90,6 +90,23 @@ public class CardPartPagedView: UIView, CardPartView { super.updateConstraints() } + + public func moveToPage(_ page: Int) { + guard page <= pageControl.numberOfPages - 1 else { return } + + UIView.animate(withDuration: 1.0) {[weak self] in + + guard let this = self else { return } + + let currentOffset = this.scrollView.contentOffset + this.scrollView.contentOffset = CGPoint(x: this.getPosition(for: page), y: currentOffset.y) + this.pageControl.currentPage = page + } + } + + private func getPosition(for page: Int) -> CGFloat { + return self.scrollView.frame.size.width * CGFloat(integerLiteral: page) + } fileprivate func updatePageControl() { diff --git a/Example/CardParts/CardPartPagedViewCardController.swift b/Example/CardParts/CardPartPagedViewCardController.swift index 1fae6a6a..9204798b 100644 --- a/Example/CardParts/CardPartPagedViewCardController.swift +++ b/Example/CardParts/CardPartPagedViewCardController.swift @@ -11,6 +11,9 @@ import CardParts class CardPartPagedViewCardController: CardPartsViewController { + private var currentPage = 0 + private var timer: Timer? + let cardPartTextView = CardPartTextView(type: .normal) let emojis: [String] = ["😎", "ðŸĪŠ", "ðŸĪĐ", "ðŸ‘ŧ", "ðŸĪŸðŸ―", "💋", "ðŸ’ƒðŸ―"] @@ -21,7 +24,7 @@ class CardPartPagedViewCardController: CardPartsViewController { var stackViews: [CardPartStackView] = [] - for i in 0...10 { + for (index, emojiString) in emojis.enumerated() { let sv = CardPartStackView() sv.axis = .vertical @@ -29,18 +32,26 @@ class CardPartPagedViewCardController: CardPartsViewController { stackViews.append(sv) let title = CardPartTextView(type: .normal) - title.text = "This is page #\(i)" + title.text = "This is page #\(index)" title.textAlignment = .center sv.addArrangedSubview(title) let emoji = CardPartTextView(type: .normal) - emoji.text = self.emojis[Int(arc4random_uniform(UInt32(self.emojis.count)))] + emoji.text = emojiString emoji.textAlignment = .center sv.addArrangedSubview(emoji) } let cardPartPagedView = CardPartPagedView(withPages: stackViews, andHeight: 200) + // To animate through the pages + timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: {[weak self] (_) in + + guard let this = self else { return } + this.currentPage = this.currentPage == this.emojis.count - 1 ? 0 : this.currentPage + 1 + cardPartPagedView.moveToPage(this.currentPage) + }) + setupCardParts([cardPartTextView, cardPartPagedView]) } } diff --git a/README.md b/README.md index b419e3e9..379f448d 100644 --- a/README.md +++ b/README.md @@ -892,6 +892,12 @@ func didMoveToPage(page: Int) Which will fire whenever the user swipes to another page +You also have the abililty to automatically move to a specific page by calling the following function on `CardPartPagedView` + +```swift +func moveToPage(_ page: Int) +``` + #### `CardPartSliderView` You can set min and max value as well as bind to the current set amount: