Button 页面控制器更改,但旋转木马不支持SwiftUI
我有这个代码,可以在点击按钮时更改转盘索引。当我点击按钮时,只有页面指示点在变化,但转盘保持不变。但当我滚动旋转页面时,指示器也会改变。我如何更新按钮内旋转木马内的页面参数,以便每当我点击按钮时,旋转木马视图将获得下一页Button 页面控制器更改,但旋转木马不支持SwiftUI,button,swiftui,carousel,Button,Swiftui,Carousel,我有这个代码,可以在点击按钮时更改转盘索引。当我点击按钮时,只有页面指示点在变化,但转盘保持不变。但当我滚动旋转页面时,指示器也会改变。我如何更新按钮内旋转木马内的页面参数,以便每当我点击按钮时,旋转木马视图将获得下一页 GeometryReader { g in Carousel(width: UIScreen.main.bounds.width, page: self.$page, height: g.frame(in: .global
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秒的视频。
在
旋转木马
视图中更新此方法
func updateUIView(_ uiView: UIScrollView, context: Context) {
uiView.setContentOffset(CGPoint(x: width * CGFloat(page), y: uiView.contentOffset.y), animated: true)
}