Advertisement
Guest User

Untitled

a guest
May 9th, 2022
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.14 KB | None | 0 0
  1. struct MovieList: View {
  2.     var movies: [Movie]
  3.    
  4.     @State var index = 0
  5.     @FocusState var selectedMovie: Int?
  6.    
  7.     var body: some View {
  8.         VStack {
  9.             Text(selectedMovie != nil ? "selected movie: \(selectedMovie!)" : "no movie selected")
  10.            
  11.             ScrollView(.horizontal, showsIndicators: false) {
  12.                 HStack(alignment: VerticalAlignment.center, spacing: 20) {
  13.                     ForEach(0..<movies.count) { i in
  14.                         MovieCard(movie: movies[i])
  15.                             .id(i)
  16.                             .focusable(true)
  17.                             .focused($selectedMovie, equals: i)
  18.                             .frame(width: getImageWidth(i), height: getImageHeight(i))
  19.                             .animation(.easeInOut, value: selectedMovie == i)
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.     }
  25.    
  26.     func getImageWidth(_ index: Int) -> CGFloat? {
  27.         return index == selectedMovie ? 250 * 1.2 : 250
  28.     }
  29.  
  30.     func getImageHeight(_ index: Int) -> CGFloat? {
  31.         return index == selectedMovie ? 400 * 1.2 : 400
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement