页面控制器发生变化但 Carousel 没有 SwiftUI
Page Controller changes but Carousel does not SwiftUI
我有这段代码可以在点击按钮时更改轮播索引。当我点击按钮时,只有页面指示器点在变化,但轮播保持不变。但是当我滚动轮播页面指示器时也会发生变化。如何更新按钮内轮播内的页面参数,以便每当我点击按钮轮播视图时都会进入下一页?
GeometryReader { g in
Carousel(width: UIScreen.main.bounds.width, page: self.$page, height: g.frame(in: .global).height)
}
Button(action: {
// self.isContinue.toggle()
if self.page+1 == Service.data.count {
self.page = 0
} else {
self.page += 1
}
}) {
Text("Devam Et")
轮播
struct Carousel: UIViewRepresentable {
var width: CGFloat
@Binding var page: Int
var height: CGFloat
func makeCoordinator() -> Coordinator {
return Carousel.Coordinator(parent1: self)
}
func makeUIView(context: Context) -> UIScrollView {
let total = width * CGFloat(Service.data.count)
let view = UIScrollView()
view.isPagingEnabled = true
view.contentSize = CGSize(width: total, height: 1)
view.bounces = true
view.showsVerticalScrollIndicator = false
view.showsHorizontalScrollIndicator = false
view.delegate = context.coordinator
let view1 = UIHostingController(rootView: ListPage(page: self.$page))
view1.view.frame = CGRect(x: 0, y: 0, width: total, height: self.height)
view1.view.backgroundColor = .clear
view.addSubview(view1.view)
return view
}
func updateUIView(_ uiView: UIScrollView, context: Context) {
}
}
列表页面
struct ListPage: View {
@Binding var page: Int
var body: some View {
HStack(spacing: 0) {
ForEach(Service.data) { i in
Card(page: self.$page, data: i, width: UIScreen.main.bounds.width)
}
}
}
}
服务
class Service: ObservableObject {
static let data: [OnboardingDetailElement] = [
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 1", subtitle: "Onboarding detail 1"),
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 2", subtitle: "Onboarding detail 2"),
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 3", subtitle: "Onboarding detail 3"),
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 4", subtitle: "Onboarding detail 4")
]
}
如果你想在这里看到问题是一个 9 秒的视频。
https://vimeo.com/498330545
在 Carousel
视图中更新此方法。
func updateUIView(_ uiView: UIScrollView, context: Context) {
uiView.setContentOffset(CGPoint(x: width * CGFloat(page), y: uiView.contentOffset.y), animated: true)
}
我有这段代码可以在点击按钮时更改轮播索引。当我点击按钮时,只有页面指示器点在变化,但轮播保持不变。但是当我滚动轮播页面指示器时也会发生变化。如何更新按钮内轮播内的页面参数,以便每当我点击按钮轮播视图时都会进入下一页?
GeometryReader { g in
Carousel(width: UIScreen.main.bounds.width, page: self.$page, height: g.frame(in: .global).height)
}
Button(action: {
// self.isContinue.toggle()
if self.page+1 == Service.data.count {
self.page = 0
} else {
self.page += 1
}
}) {
Text("Devam Et")
轮播
struct Carousel: UIViewRepresentable {
var width: CGFloat
@Binding var page: Int
var height: CGFloat
func makeCoordinator() -> Coordinator {
return Carousel.Coordinator(parent1: self)
}
func makeUIView(context: Context) -> UIScrollView {
let total = width * CGFloat(Service.data.count)
let view = UIScrollView()
view.isPagingEnabled = true
view.contentSize = CGSize(width: total, height: 1)
view.bounces = true
view.showsVerticalScrollIndicator = false
view.showsHorizontalScrollIndicator = false
view.delegate = context.coordinator
let view1 = UIHostingController(rootView: ListPage(page: self.$page))
view1.view.frame = CGRect(x: 0, y: 0, width: total, height: self.height)
view1.view.backgroundColor = .clear
view.addSubview(view1.view)
return view
}
func updateUIView(_ uiView: UIScrollView, context: Context) {
}
}
列表页面
struct ListPage: View {
@Binding var page: Int
var body: some View {
HStack(spacing: 0) {
ForEach(Service.data) { i in
Card(page: self.$page, data: i, width: UIScreen.main.bounds.width)
}
}
}
}
服务
class Service: ObservableObject {
static let data: [OnboardingDetailElement] = [
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 1", subtitle: "Onboarding detail 1"),
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 2", subtitle: "Onboarding detail 2"),
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 3", subtitle: "Onboarding detail 3"),
OnboardingDetailElement(image: "OnboardingImageOne", title: "Onboarding title 4", subtitle: "Onboarding detail 4")
]
}
如果你想在这里看到问题是一个 9 秒的视频。 https://vimeo.com/498330545
在 Carousel
视图中更新此方法。
func updateUIView(_ uiView: UIScrollView, context: Context) {
uiView.setContentOffset(CGPoint(x: width * CGFloat(page), y: uiView.contentOffset.y), animated: true)
}