如何在SwiftUI中更新页面指示器?
界面:SwiftUI如何在SwiftUI中更新页面指示器?,swift,swiftui,uipageviewcontroller,uipagecontrol,uiviewrepresentable,Swift,Swiftui,Uipageviewcontroller,Uipagecontrol,Uiviewrepresentable,界面:SwiftUI 生命周期:迅捷 语言:Swift Xcode版本:12.5 我想在具有核心数据的自定义旋转木马视图中使用页面指示器。指示器与下面的当前代码配合良好,但如果从核心数据中添加或删除页面,指示器的数量不会改变 添加或删除页面时,pages.count会更改,但我检查了func makeUIView()是否不再执行 当self.pages.count的值更改时,如何重新加载func makeUIView automatic struct PageControl : UIViewRe
生命周期:迅捷
语言:Swift
Xcode版本:12.5 我想在具有核心数据的自定义旋转木马视图中使用页面指示器。指示器与下面的当前代码配合良好,但如果从核心数据中添加或删除页面,指示器的数量不会改变 添加或删除页面时,pages.count会更改,但我检查了func makeUIView()是否不再执行 当self.pages.count的值更改时,如何重新加载func makeUIView automatic
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)
}
}
}
struct PageControl:UIViewRepresentable{
@环境对象变量ani:旋转木马模型
@FetchRequest(实体:Page.entity(),
SortDescriptor:[NSSortDescriptor(键路径:\Page.name,升序:true)]
)变量页面:FetchedResults
func makeUIView(上下文:context)->UIPageControl{
let view=UIPageControl()
view.currentPageIndicatorTintColor=UIColor(红色:100/255,绿色:255/255,蓝色:177/255,alpha:1.0)
view.pageIndicatorTintColor=UIColor(红色:100/255,绿色:255/255,蓝色:177/255,alpha:1.0)。带alpha组件(0.2)
view.numberOfPages=self.pages.count+1
返回视图
}
func updateUIView(uiView:UIPageControl,context:context){
//页面更改时更新页面指示器。。。
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
}
}