Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kyome22/infinitepaging
This provides infinite carousel-like paging view in SwiftUI.
https://github.com/kyome22/infinitepaging
Last synced: about 2 months ago
JSON representation
This provides infinite carousel-like paging view in SwiftUI.
- Host: GitHub
- URL: https://github.com/kyome22/infinitepaging
- Owner: Kyome22
- License: mit
- Created: 2023-10-22T03:32:00.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-28T16:17:19.000Z (10 months ago)
- Last Synced: 2024-10-31T22:52:00.213Z (about 2 months ago)
- Language: Swift
- Homepage:
- Size: 666 KB
- Stars: 30
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# InfinitePaging
This provides infinite carousel-like paging view in SwiftUI.
## Requirements
- Development with Xcode 15.2+
- Written in Swift 5.9
- swift-tools-version: 5.9
- Compatible with iOS 16.4+## Usage
1. Define a structure conforming to Pageable.
```swift
import Foundation
import InfinitePagingstruct Page: Pageable {
var id = UUID()
var number: Int
}
```2. Use InfinitePagingView
```swift
import SwiftUI
import InfinitePagingstruct ContentView: View {
// Prepare three elements to display at first.
@State var pages: [Page] = [
Page(number: -1),
Page(number: 0),
Page(number: 1)
]var body: some View {
InfinitePagingView(
objects: $pages,
pageAlignment: .horizontal,
pagingHandler: { pageDirection in
paging(pageDirection)
},
content: { page in
pageView(page)
}
)
}// Define the View that makes up one page.
private func pageView(_ page: Page) -> some View {
return Text(String(page.number))
.font(.largeTitle)
.fontWeight(.bold)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
.clipShape(RoundedRectangle(cornerRadius: 32))
.padding()
}// Shifts the array element by one when a paging request comes.
private func paging(_ pageDirection: PageDirection) {
switch pageDirection {
case .backward:
if let number = pages.first?.number {
pages.insert(Page(number: number - 1), at: 0)
pages.removeLast()
}
case .forward:
if let number = pages.last?.number {
pages.append(Page(number: number + 1))
pages.removeFirst()
}
}
}
}
```