Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/LePips/CollectionView
Basic SwiftUI CollectionView
https://github.com/LePips/CollectionView
ios swift swiftui tvos
Last synced: 15 days ago
JSON representation
Basic SwiftUI CollectionView
- Host: GitHub
- URL: https://github.com/LePips/CollectionView
- Owner: LePips
- License: mit
- Created: 2022-08-24T17:45:16.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-26T01:05:07.000Z (about 2 years ago)
- Last Synced: 2023-03-07T13:18:53.853Z (over 1 year ago)
- Topics: ios, swift, swiftui, tvos
- Language: Swift
- Homepage:
- Size: 27.3 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CollectionView
A basic wrapper of `UICollectionView` to overcome `LazyVGrid/LazyHGrid` performance issues.
Influenced by [SwiftUICollection](https://github.com/defagos/SwiftUICollection) and [ASCollectionView](https://github.com/apptekstudios/ASCollectionView).
# Usage
Currently, `CollectionView` just acts to display items for performance improvements. Further enhancements may be implemented in the future.
iOS Example
```swift
struct ContentView: View {
@State
var items = (0..<100).map { "\($0)" }
static let colors: [Color] = [.blue, .red, .green, .yellow, .purple, .cyan, .indigo, .mint, .orange]
var body: some View {
CollectionView(items: items) { indexPath, item, proxy in
Button {
if indexPath.row < 50 {
proxy.scrollTo(.bottom)
} else {
proxy.scrollTo(.top)
}
} label: {
ZStack {
Self.colors.randomElement()
.frame(height: 150)
.cornerRadius(10)
Text(item)
}
}
}
.willReachEdge(insets: .init(top: 300, leading: 0, bottom: 300, trailing: 0)) { edge in
print("Will reach edge: \(edge)")
}
.onEdgeReached { edge in
print("Edge reached: \(edge)")
}
.layout { _, layoutEnvironment in
.grid(layoutEnvironment: layoutEnvironment,
layoutMode: .adaptive(withMinItemSize: 100),
sectionInsets: .zero)
}
.configure { configuration in
configuration.showsVerticalScrollIndicator = false
}
.ignoresSafeArea()
}
}
```tvOS Example
```swift
struct ContentView: View {
@State
var items = (0..<15).map { "\($0)" }
static let colors: [Color] = [.blue, .red, .green, .yellow, .purple, .cyan, .indigo, .mint, .orange]
var body: some View {
CollectionView(items: items) { _, item, _ in
Button {
items.append("\(items.count)")
} label: {
ZStack {
Self.colors.randomElement()
.frame(width: 200, height: 200)
Text(item)
}
}
.buttonStyle(CardButtonStyle())
}
.willReachEdge(insets: .init(top: 300, leading: 0, bottom: 300, trailing: 0)) { edge in
print("Will reach edge: \(edge)")
}
.onEdgeReached { edge in
print("Edge reached: \(edge)")
}
.layout { _, layoutEnvironment in
.grid(layoutEnvironment: layoutEnvironment,
layoutMode: .adaptive(withMinItemSize: 200),
itemSpacing: 60,
lineSpacing: 40,
itemSize: .estimated(200))
}
.configure { configuration in
configuration.showsVerticalScrollIndicator = false
}
.ignoresSafeArea()
}
}
```