Reputation: 1480
I can't added gif. I uploaded YouTube. Link here.
When Tabview is swipe, I want to show that image in imageView below.
Note: My code didn't work when I tried it with the model. That's why I edited my question.
MainView
struct MainView: View {
@State var selected: Movie = Movie(title: "", description: "", poster: "", thumbImage: [], video: "", positiveRate: 0, negativeRate: 0)
var body: some View {
ZStack {
Image(selected.poster)
.resizable()
.overlay(Blur().edgesIgnoringSafeArea(.all))
VStack {
TabView(selection: $selected) {
ForEach(movieList, id: \.id) { item in
MovieCell(movie: item)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
}
}
}
}
Model
struct Movie: Identifiable, Hashable {
var id = UUID()
var title: String
var description: String
var poster: String
var thumbImage: [String]
var video: String
var positiveRate: Int
var negativeRate: Int
}
Example Data
let movieList = [
Movie(title: "Minyonlar 2", description: "In the heart of the 1970s, amidst a flurry of feathered hair and flared jeans, Gru (Steve Carell) is growing up in the suburbs. A fanboy of a supervillain supergroup known as the Vicious 6, Gru hatches a plan to become evil enough to join them. Luckily, he gets some mayhem-making back-up from his loyal followers, the Minions. Together, Kevin, Stuart, Bob, and Otto - a new Minion sporting braces and a desperate need to please - deploy their skills as they and Gru build their first lair, experiment with their first weapons, and pull off their first missions. When the Vicious 6 oust their leader, legendary fighter Wild Knuckles (Alan Arkin), Gru interviews to become their newest member. It doesn't go well (to say the least), and only gets worse after Gru outsmarts them and suddenly finds himself the mortal enemy of the apex of evil. On the run, Gru will turn to an unlikely source for guidance, Wild Knuckles, and discover that even bad guys need a little help from their friends. Written by Universal Pictures", poster: "2", thumbImage: ["minions/m1","minions/m2","minions/m3"], video: Bundle.main.path(forResource: "minyonlar", ofType: "mp4")!, positiveRate: 150, negativeRate: 5, categoryType: .animation),
Movie(title: "Hiçkimse", description: "Emmy winner Bob Odenkirk (Better Call Saul, The Post, Nebraska) stars as Hutch Mansell, an underestimated and overlooked dad and husband, taking life's indignities on the chin and never pushing back. A nobody. When two thieves break into his suburban home one night, Hutch declines to defend himself or his family, hoping to prevent serious violence. His teenage son, Blake (Gage Munroe, The Shack), is disappointed in him and his wife, Becca (Connie Nielsen, Wonder Woman), seems to pull only further away. The aftermath of the incident strikes a match to Hutch's long-simmering rage, triggering dormant instincts and propelling him on a brutal path that will surface dark secrets and lethal skills. In a barrage of fists, gunfire and squealing tires, Hutch must save his family from a dangerous adversary (famed Russian actor Aleksey Serebryakov, Amazon's McMafia)-and ensure that he will never be underestimated as a nobody again. Written by Universal Pictures", poster: "1", thumbImage: ["hickimse/h1","hickimse/h2","hickimse/h3"], video: Bundle.main.path(forResource: "hickimse", ofType: "mp4")!, positiveRate: 100, negativeRate: 250, categoryType: .action),
Movie(title: "Six Minutes of Midnight", description: "Summer 1939. Influential families in Nazi Germany have sent their daughters to a finishing school in an English seaside town to learn the language and be ambassadors for a future looking National Socialist. A teacher there sees what is coming and is trying to raise the alarm. But the authorities believe he is the problem. Written by Andy Evans", poster: "3", thumbImage: ["sixminutes/s1"], video: Bundle.main.path(forResource: "sixMinutes", ofType: "mp4")!, positiveRate: 80, negativeRate: 60, categoryType: .war)
]
Upvotes: 3
Views: 2052
Reputation: 872
Create a @State
property to get index
@State private var selected: Int = 0
This will return the selected index. Tested this code and works as expected.
Upvotes: 0
Reputation: 54466
You need to add tag
to your TabView items. Note that these tags must be of type Movie
- the same type as the selected
property.
struct MainView: View {
@State var selected: Movie = movieList[0] // set the selection to the first item
var body: some View {
ZStack {
Image(selected.poster)
.resizable()
.overlay(Blur().edgesIgnoringSafeArea(.all))
VStack {
TabView(selection: $selected) {
ForEach(movieList, id: \.id) { item in
MovieCell(movie: item)
.tag(item) // `tag` items here
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
}
}
}
}
Upvotes: 0
Reputation: 257693
Just specify selection in image, like
Image(selected) // << here !1
.resizable()
.frame(width: 150, height: 200)
Tested on replicated code with Xcode 12.4 / iOS 14.4
Upvotes: 1