Comments (4)
I'm gonna start supporting RandomAccessCollection
with Int
indexes on version 1.13.0. I do the conversion to an Array
internally. See version 1.13.0-beta.1
As far as I can see in your gist, this would be enough for you. Then, if you implement onPageChanged
, you could call your updateCurrentIndex
in this callback:
@State var page = 0 // set here the start index
@State var data: MyRandomAccessCollectionImplementation
Pager(page: $page, data: data, id: \.id) {
// return your View here
}
.onPageChanged { index in
data.updateCurrentIndex(index)
}
from swiftuipager.
@TheNoim I'm a bit confused by "A way to use this without passing an array". How is Pager
supposed to know what populates the pages, otherwise? What you're seeing as an argument of the view builder is not an index but a data item. This is needed to populate the page.
All in all, the way you describe Pager
behavior is incorrect. In the example provided, which I think you've got from the documentation and I'm gonna rename, index
it's not just a random integer but a data item. So some might want to page integers from 0 to 10, some other users might want to iterate them from 10 to 20, some others might want to iterate months as you want. This is why "A way to use this without passing an array" doesn't quite makes sense. Pager
takes an data array, a binding and a view builder block. The array is used to build the page at a specific index (that is, next/previous pages). The binding allows you to keep track of the current index.
Just for further clarification:
@State var page: Int = 0
var items = Array(0..<10)
var body: some View {
Pager(page: $page,
data: items,
id: \.identifier,
content: { index in
//
// This isn't just an index. It's an item from data. In this example is an integer
//
Text("Page: \(index)")
})
}
What you would need to do is something like this:
// 9 as we're in October
@State var page: Int = CalendaryMonth.current.monthIndex
@State var oldPage: Int = CalendaryMonth.current.monthIndex
@State var year = 2020
let months = [CalendarMonth(.january), ..., CalendarMonth(.october), CalendarMonth(.november), CalendarMonth(.december)]
var body: some View {
Pager(page: $page,
data: months,
id: \.monthIndex,
content: { month in
// Render month accordingly depending on the year
CalendarViewMonth(month: month, year: year)
})
.loopPages()
.onPageChanged { newPage in
if newPage == 0 && page == 11 { year += 1 }
if newPage == 11 && oldPage == 0 { year -= 1 }
oldPage = newPage
}
}
from swiftuipager.
Pager takes an data array, a binding and a view builder block. The array is used to build the page at a specific index (that is, next/previous pages). The binding allows you to keep track of the current index.
This is not really against my post. My suggestion was more like this:
Allow to pass an object conforming to the RandomAccessCollection protocol. This would allow to define your own end and start of Indicies and even updating them dynamically. Currently, you can only pass fixed Data with the index start of zero and a known index end. It would be much cooler if the pager gets no data and the content gets rendered completely dynamic with a passed index. For an infinitive scenario, the pager doesn't need to know the length of an array. It only needs to render the last, the current and next item. It doesn't really need to know what kind of data it is or how many items the data source contains.
from swiftuipager.
I'm not following, could you please show me an example? Even just pseudo-code. I wanna see how you dynamically add data and change the index.
As far as I know, I think you could have a @State
variable for you data
and change it dynamically. This is what I do in the example code, InfiniteExampleView.swift, where I append more items on the fly. If going backwards, you could detect that (the way I was doing for year
in my previous snippet) and insert more items in your array while incrementing the index (so if you append 5 previous months, then your new index is your current index plus 5).
from swiftuipager.
Related Issues (20)
- [BUG] Weird moving transparent Image appearing when scrolling fast.
- [BUG] tvOS NavigationLink not work HOT 1
- [FEAT] Hide scroll indicator on watchOS
- [BUG] To many pageView request
- [BUG] The element inside of Pager is hidden when when it goes beyond the screen border
- [FEAT] Page Indicator dots (`PageTabViewStyle.IndexDisplayMode`) at the bottom according page count HOT 2
- Pager not shown when using inside ScrollView HOT 3
- Pager Size according to the inner view
- [BUG] App Crashes detected HOT 2
- [BUG] Cant swipe, the page goes back to first
- [BUG] Item's with different size does not work as expected for swipeDirection vertical
- Auto Scroll horizontally one item at a time when user is not interacting
- [FEAT] Ability to not clip children HOT 1
- Multiple Page nesting causes sliding bug
- [FEAT] .dragBackwardsOnly() ? HOT 1
- [BUG] macOS 14 displays focusRing HOT 2
- [FEAT] The anchor point for interactive scaling
- [FEAT] Interactive Blur Effect
- [BUG] Pager scrolls in the opposite direction in RLT languages
- [FEAT] Do not deallocate page when scrolling to next page
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftuipager.