Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add move to page functionality #227

Merged
merged 1 commit into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CardParts/src/Classes/Card Parts/CardPartPagedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
17 changes: 14 additions & 3 deletions Example/CardParts/CardPartPagedViewCardController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ["😎", "🤪", "🤩", "👻", "🤟🏽", "💋", "💃🏽"]

Expand All @@ -21,26 +24,34 @@ class CardPartPagedViewCardController: CardPartsViewController {

var stackViews: [CardPartStackView] = []

for i in 0...10 {
for (index, emojiString) in emojis.enumerated() {

let sv = CardPartStackView()
sv.axis = .vertical
sv.spacing = 8
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])
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down