SwiftUI - 如何将选择添加到 TabView
SwiftUI - How to add selection to TabView
我有一个 JSON API,我想显示 PageTabView 中的内容。它是成功的,但是每次我滑动页面时视图都会变慢,我在这里得到了解决方案。但现在的问题是,每次我滑动页面时,页面点都不想改变。我已经尝试解决好几天了,但仍然找不到解决方案,这是我的代码(我无法显示 API link,因为它不可共享)
struct HomeBanner: View{
@State var banners: [BannerJSON] = []
private let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
@State var index = 0
var body: some View{
let bannerPic = "API_LINK"
GeometryReader{ geometry in
VStack(spacing: 2){
if banners.isEmpty{
Spacer()
ProgressView()
Spacer()
}else{
VStack{
ZStack{
TabView(selection: $index){
ForEach(banners, id: \.id){banner in
VStack{
//MARK: Display Banner
NavigationLink(destination: DetailBanner(banner: banner
)){
WebImage(url: URL(string: bannerPic + banner.IMAGE))
.resizable()
.scaledToFit()
}
}
}
}.tabViewStyle(PageTabViewStyle())
.frame(width: geometry.size.width, height: geometry.size.height / 3.5)
}
}
}
}.onAppear{
getBannerData(url: "API_LINK"){
(banners) in
self.banners = banners
}
}
}
}
}
struct BannerJSON{
var RECORD_STATUS: String
var IMAGE: String
var URL_LINK: String
var TITLE: String
var DESCRIPTION: String
}
extension BannerJSON: Identifiable, Decodable{
var id: String {return TITLE}
}
struct SampleTabView: View {
@State var selectedIndex: Int = 0
@State var banners: [Int] = [0,1,2,3,4,5]
var body: some View {
TabView(selection: $selectedIndex){
ForEach(0..<banners.count, id: \.self){ idx in
Text(banners[idx].description)
.tag(idx)//This line is crutial for selection to work put in on the VStack
//You can also make the tag the banner in your loop but your selection variable must be a Banner too.
}
}
}
}
我有一个 JSON API,我想显示 PageTabView 中的内容。它是成功的,但是每次我滑动页面时视图都会变慢,我在这里得到了解决方案。但现在的问题是,每次我滑动页面时,页面点都不想改变。我已经尝试解决好几天了,但仍然找不到解决方案,这是我的代码(我无法显示 API link,因为它不可共享)
struct HomeBanner: View{
@State var banners: [BannerJSON] = []
private let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
@State var index = 0
var body: some View{
let bannerPic = "API_LINK"
GeometryReader{ geometry in
VStack(spacing: 2){
if banners.isEmpty{
Spacer()
ProgressView()
Spacer()
}else{
VStack{
ZStack{
TabView(selection: $index){
ForEach(banners, id: \.id){banner in
VStack{
//MARK: Display Banner
NavigationLink(destination: DetailBanner(banner: banner
)){
WebImage(url: URL(string: bannerPic + banner.IMAGE))
.resizable()
.scaledToFit()
}
}
}
}.tabViewStyle(PageTabViewStyle())
.frame(width: geometry.size.width, height: geometry.size.height / 3.5)
}
}
}
}.onAppear{
getBannerData(url: "API_LINK"){
(banners) in
self.banners = banners
}
}
}
}
}
struct BannerJSON{
var RECORD_STATUS: String
var IMAGE: String
var URL_LINK: String
var TITLE: String
var DESCRIPTION: String
}
extension BannerJSON: Identifiable, Decodable{
var id: String {return TITLE}
}
struct SampleTabView: View {
@State var selectedIndex: Int = 0
@State var banners: [Int] = [0,1,2,3,4,5]
var body: some View {
TabView(selection: $selectedIndex){
ForEach(0..<banners.count, id: \.self){ idx in
Text(banners[idx].description)
.tag(idx)//This line is crutial for selection to work put in on the VStack
//You can also make the tag the banner in your loop but your selection variable must be a Banner too.
}
}
}
}