页面控制器发生了变化,但 Carousel 没有 SwiftUI
Posted
技术标签:
【中文标题】页面控制器发生了变化,但 Carousel 没有 SwiftUI【英文标题】:Page Controller changes but Carousel does not SwiftUI 【发布时间】:2021-01-08 12:44:40 【问题描述】:我有这段代码可以在点击按钮时更改轮播索引。当我点击按钮时,只有页面指示点发生变化,但轮播保持不变。但是当我滚动轮播页面指示器时也会发生变化。如何更新按钮内轮播内的页面参数,以便每当我点击按钮轮播视图时都会获得下一页?
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
【问题讨论】:
【参考方案1】:在Carousel
视图中更新此方法。
func updateUIView(_ uiView: UIScrollView, context: Context)
uiView.setContentOffset(CGPoint(x: width * CGFloat(page), y: uiView.contentOffset.y), animated: true)
【讨论】:
以上是关于页面控制器发生了变化,但 Carousel 没有 SwiftUI的主要内容,如果未能解决你的问题,请参考以下文章
适配器的内容发生了变化,但 ListView 没有收到通知。安卓问题