Closed SpectatorNan closed 3 years ago
I think this is easy enough to implement by using a Timer
. It makes more sense to implement it on the client's side rather inside the library. Reason is Pager
might be redrawn if there's a state change in the container's view. You'd need to something like this:
struct MyView: View {
@StateObject var page: Page = .first()
var body: some View {
Pager(...)
.disableDragging()
.onAppear {
Timer
.publish(
every: 3.0, // Use desired interval
on: .main,
in: .default
)
.sink { _ in
withAnimation { page.update(.next)) } // use desired animation and desired page increment
}
}
}
}
Confirmed, it's not reliable to have a Timer
set inside Pager
as the container view might redraw it and then the count is lost. Especially if you combine several Pager
in the same screen, one will trigger an update of the other.
See Hacking with Swift for reference of how to use timers with Combine. Alternatively you can use the "old way":
yeah, I have tried to customize a simple pager and found that the redraw caused the timer to be created repeatedly. Causes the scrolling effect to fail.
I am trying and learning SwiftUI and Combine to write an APP, and there is a lot of thinking that has not been converted from UIKit. Thank you for your answer
I found that after the view toggles through the tabview, the scrolling is abnormal
By breakpoint discovery, the onReceive was received, but no scrolling animation was performed after the Page Update was executed
current version: 2.0.0
I will continue to try this
my view code:
Pager(page: page, data: Array(0..<selectModel.banners.count), id: \.self) { (idx) in
bannerView(banner: selectModel.banners[idx]).cornerRadius(3) // custom image view...
}
.allowsDragging()
.loopPages()
.pagingAnimation({ currentPage, nextPage, totalShift, velocity in
return PagingAnimation.custom(animation: .linear)
})
.onPageWillChange({ (newIdx) in
SNLog("will page - \(newIdx)")
})
.onPageChanged({ (newIndex) in
// do something
SNLog("page - \(newIndex)")
})
.itemSpacing(10.fit)
.onReceive(timer) { _ in
withAnimation {
page.update(.next)
}
}.frame(height: bannerHeight)
Is your feature request related to a problem? Please describe.
i want the pager content automatic scroll, for example, fspagerview : pagerView.automaticSlidingInterval = 3.0 auto scroll every 3 seconds;