Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Created April 4, 2020 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfonsodev/411622f65727bd5f44eff13f6e7e0a9b to your computer and use it in GitHub Desktop.
Save alfonsodev/411622f65727bd5f44eff13f6e7e0a9b to your computer and use it in GitHub Desktop.
SWiftUI example
//
// ContentView.swift
//
import AVFoundation
import SwiftUI
struct Track: Identifiable {
var id = UUID()
var title: String
}
let playlist: [Track] = [
Track(title: "Song 1"),
Track(title: "Song 2"),
Track(title: "Song 3"),
]
struct ContentView: View {
@State private var playlistIndex = 0
var body: some View {
VStack {
HStack {
Text("playlistIndex: \(self.playlistIndex)")
}
VStack {
ForEach(0..<playlist.count) {
if $0 == self.playlistIndex {
Text("🔈 \(playlist[$0].title)")
} else {
Text(playlist[$0].title)
}
}
}.padding()
HStack {
Text("⏮").onTapGesture {
if self.playlistIndex == 0 {
self.playlistIndex = playlist.count - 1
} else {
self.playlistIndex = self.playlistIndex - 1
}
}
Text("⏭").onTapGesture {
if self.playlistIndex == playlist.count - 1 {
self.playlistIndex = 0
} else {
self.playlistIndex = self.playlistIndex + 1
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment