我正在快速构建一个音乐播放器应用程序。我有“MainView”,其中专辑通过数组显示,“AlbumView”将显示一个新窗口,用户可以在其中播放专辑。我只想在用户点击“MainView”中的相册时显示“AlbumView”。这是自从我看到专辑名称改变颜色并且没有错误但当我点击专辑时没有任何反应以来我得到的最接近的。也试过模拟器。应用程序布局的更多上下文。我在 MainView 中工作,但当应用程序运行时,它会打开 TabbedView,这是一个包含 MainView 和 TabbarView 的文件。我试图导航到的相册视图是一个包含垂直堆叠的 AlbumHeader 和 AlbumContentView 的文件。所以不确定其他视图中是否有我需要修改的代码。
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 32) {
ForEach(Array(zip(mainViewModel.artistsOfTheWeek.indices, mainViewModel.artistsOfTheWeek)), id: \.0) { index, artist in
NavigationLink(destination: AlbumView(playerOpened: $playerOpened)) { ArtistView(artist: artist)
.padding(.top, 12)
.padding(.leading, 4)
.opacity(self.animateArtists ? 1 : 0)
.scaleEffect(self.animateArtists ? 1 : 0.5)
.animation(.easeInOut(duration: 0.3).delay(0.1 * Double(index)), value: self.animateArtists)
}
}
}
}
这里有一种方法可以做到这一点。可以先定义两个状态变量
@State private var showAlbumView = false
@State private var selectedAlbum = // to store either the index or the artist
然后在代码块中,
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 30) {
ForEach(Array(zip(mainViewModel.artistsOfTheWeek.indices, mainViewModel.artistsOfTheWeek)), id: \.0) { (index, artist) in
Button(action: {
showAlbumView.toggle()
selectedAlbum = // store index or artist
}) {
ArtistView(artist: artist)
.padding(.top, 10)
.padding(.leading, 4)
.opacity(self.animateArtists ? 1 : 0)
.scaleEffect(self.animateArtists ? 1 : 0.5)
.animation(.easeInOut(duration: 0.3).delay(0.1 * Double(index)), value: self.animateArtists)
}.fullScreenCover(isPresented: $showAlbumView) {
AlbumView(\\ pass the selectedAlbum here so that you can show the corresponding info in this view)
}
}
}
}
如果您有任何问题,请告诉我!