如何在 SwiftUI 中更新页面指示器?
How to update page indicator in SwiftUI?
接口:SwiftUI
生命周期:SwiftUI
语言:Swift
Xcode版本:12.5
我想在带有核心数据的自定义轮播视图中使用页面指示器。该指标与下面的当前代码一起工作正常,但如果从 Core Data 添加或删除页面,指标的数量不会改变。
添加或删除页面时,pages.count发生了变化,但我检查了func makeUIView()没有再次执行
当 self.pages.count 的值发生变化时,如何自动重新加载 func makeUIView?
struct PageControl : UIViewRepresentable {
@EnvironmentObject var ani: CarouselModel
@FetchRequest(entity: Page.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Page.name, ascending: true)]
) var pages: FetchedResults<Page>
func makeUIView(context: Context) -> UIPageControl {
let view = UIPageControl()
view.currentPageIndicatorTintColor = UIColor(red: 100/255, green: 255/255, blue: 177/255, alpha: 1.0)
view.pageIndicatorTintColor = UIColor(red: 100/255, green: 255/255, blue: 177/255, alpha: 1.0).withAlphaComponent(0.2)
view.numberOfPages = self.pages.count + 1
return view
}
func updateUIView(_ uiView: UIPageControl, context: Context) {
// Updating Page Indicator When Ever Page Changes...
DispatchQueue.main.async {
uiView.currentPage = Int(self.ani.count)
}
}
}
我明白了!
将下面的行从 func makeUIView 放到 func updateUIView
uiView.numberOfPages = pages.count + 1
决赛
func updateUIView(_ uiView: UIPageControl, context: Context) {
// Updating Page Indicator When Ever Page Changes...
DispatchQueue.main.async {
uiView.currentPage = Int(self.ani.count)
uiView.numberOfPages = pages.count + 1
}
}
接口:SwiftUI
生命周期:SwiftUI
语言:Swift
Xcode版本:12.5
我想在带有核心数据的自定义轮播视图中使用页面指示器。该指标与下面的当前代码一起工作正常,但如果从 Core Data 添加或删除页面,指标的数量不会改变。
添加或删除页面时,pages.count发生了变化,但我检查了func makeUIView()没有再次执行
当 self.pages.count 的值发生变化时,如何自动重新加载 func makeUIView?
struct PageControl : UIViewRepresentable {
@EnvironmentObject var ani: CarouselModel
@FetchRequest(entity: Page.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Page.name, ascending: true)]
) var pages: FetchedResults<Page>
func makeUIView(context: Context) -> UIPageControl {
let view = UIPageControl()
view.currentPageIndicatorTintColor = UIColor(red: 100/255, green: 255/255, blue: 177/255, alpha: 1.0)
view.pageIndicatorTintColor = UIColor(red: 100/255, green: 255/255, blue: 177/255, alpha: 1.0).withAlphaComponent(0.2)
view.numberOfPages = self.pages.count + 1
return view
}
func updateUIView(_ uiView: UIPageControl, context: Context) {
// Updating Page Indicator When Ever Page Changes...
DispatchQueue.main.async {
uiView.currentPage = Int(self.ani.count)
}
}
}
我明白了!
将下面的行从 func makeUIView 放到 func updateUIView
uiView.numberOfPages = pages.count + 1
决赛
func updateUIView(_ uiView: UIPageControl, context: Context) {
// Updating Page Indicator When Ever Page Changes...
DispatchQueue.main.async {
uiView.currentPage = Int(self.ani.count)
uiView.numberOfPages = pages.count + 1
}
}