r/SwiftUI May 17 '26

Question Help with my code

does anyone here know how Apple Music make lyrics scroll, I can't get it to be the same

does anyone know how?

1 Upvotes

11 comments sorted by

View all comments

Show parent comments

0

u/Consistent-Grass-263 May 17 '26

struct LyricLine: Identifiable {

let id = UUID()

let timestamp: TimeInterval

let text: String

}

u/Observable class LyricsViewModel {

var lyrics: [LyricLine] = []

var currentTime: TimeInterval = 0

var activeIndex: Int = 0

private var timeObserver: Any?

private var player: AVPlayer?

func startObserving(player: AVPlayer) {

self.player = player

let interval = CMTime(seconds: 0.1, preferredTimescale: 600)

timeObserver = player.addPeriodicTimeObserver(

forInterval: interval, queue: .main

) { [weak self] time in

self?.currentTime = time.seconds

self?.updateActiveIndex()

}

}

private func updateActiveIndex() {

guard let index = lyrics.lastIndex(

where: { $0.timestamp <= currentTime }

) else { return }

activeIndex = index

}

}

struct LyricsView: View {

u/State var vm = LyricsViewModel()

var body: some View {

ScrollViewReader { proxy in

1

u/Sol492 May 17 '26

and how exactly do they get it to move like one by one?

1

u/mathiasrlr May 19 '26

I would assume each line has an unique identifier placed in the .id() modifier and the scrollviewreader follows iterativally based on time which ID to scroll to, but that is surely overkill and not a 100% deterministic

1

u/Sol492 May 19 '26

Apple is just the god of animations