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

Improve Page View #61

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions Modules/PageView/PageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class PageView<ContentView: StatefulViewProtocol>: StatefulView<Pag
private lazy var scrollView: UIScrollView = .instantiate()
private lazy var contentViews: [ContentView] = []
private var contentViewIndex: Int = 0
private var verticalScrollEnabled: Bool = true

public override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -35,6 +36,7 @@ public final class PageView<ContentView: StatefulViewProtocol>: StatefulView<Pag
public override func didChangeModel() {
super.didChangeModel()

verticalScrollEnabled = model.verticalScrollEnabled
switch model.axis {
case .horizontal:
scrollView.alwaysBounceHorizontal = true
Expand Down Expand Up @@ -65,6 +67,14 @@ public final class PageView<ContentView: StatefulViewProtocol>: StatefulView<Pag
updateContentLayout()
}

public func scrollToPage(atIndex index: Int, animated: Bool = true) {
let contentOffset = CGPoint(
x: CGFloat(index) * bounds.width,
y: scrollView.contentOffset.y
)
scrollView.setContentOffset(contentOffset, animated: animated)
}

private func updateContentLayout() {
let totalContentDimension = CGFloat(contentViews.count) * min(bounds.width, bounds.height)
scrollView.contentSize = CGSize(
Expand All @@ -82,6 +92,9 @@ public final class PageView<ContentView: StatefulViewProtocol>: StatefulView<Pag
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if((scrollView.contentOffset.y > 0 || scrollView.contentOffset.y < 0) && !verticalScrollEnabled) {
scrollView.contentOffset.y = 0
}
let newContentViewIndex: Int
switch model.axis {
case .horizontal:
Expand Down
13 changes: 10 additions & 3 deletions Modules/PageView/PageViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,25 @@ public struct PageViewModel<Content: ViewModelProtocol>: ViewModelProtocol {
/// The callback for page index changes.
public let onPageIndexChanged: PageIndexChangeCallback

/// Wheter the PageView is vertically scrollable or not.
public let verticalScrollEnabled: Bool

/// The default initializer of `PageViewModel`.
///
///
/// - Parameter axis: The scroll axis for the paging behaviour.
/// - Parameter pages: The paginated content view models.
/// - Parameter onPageIndexChanged: The content view models for the paginated content views.
/// - Parameter verticalScrollEnabled: Whether the pageview is scrolling verticaly or not.
public init(
axis: Axis = PageViewModel.default.axis,
pages: [Content] = PageViewModel.default.pages,
onPageIndexChanged: @escaping PageIndexChangeCallback = PageViewModel.default.onPageIndexChanged
onPageIndexChanged: @escaping PageIndexChangeCallback = PageViewModel.default.onPageIndexChanged,
verticalScrollEnbaled: Bool = PageViewModel.default.verticalScrollEnabled
) {
self.axis = axis
self.pages = pages
self.onPageIndexChanged = onPageIndexChanged
self.verticalScrollEnabled = verticalScrollEnbaled
}
}

Expand All @@ -47,7 +53,8 @@ extension PageViewModel {
return .init(
axis: .horizontal,
pages: [],
onPageIndexChanged: { _ in }
onPageIndexChanged: { _ in },
verticalScrollEnbaled: true
)
}
}