Skip to content

Instantly share code, notes, and snippets.

@Pobe16
Last active June 12, 2020 14:14
Show Gist options
  • Save Pobe16/f1928480fcedaaddc1db6b22cf02e822 to your computer and use it in GitHub Desktop.
Save Pobe16/f1928480fcedaaddc1db6b22cf02e822 to your computer and use it in GitHub Desktop.
YTViewRepresentable step 1
import SwiftUI
import UIKit
import YouTubePlayer
// 1
struct YouTubeView: UIViewRepresentable {
// 2
typealias UIViewType = YouTubePlayerView
// 3
@ObservedObject var playerState: YouTubeControlState
// 4
init(playerState: YouTubeControlState) {
self.playerState = playerState
}
// 5
func makeUIView(context: Context) -> UIViewType {
let playerVars = []
let ytVideo = YouTubePlayerView()
ytVideo.playerVars = playerVars as YouTubePlayerView.YouTubePlayerParameters
return ytVideo
}
// 6
func updateUIView(_ uiView: UIViewType, context: Context) {
guard let videoID = playerState.videoID else { return }
uiView.loadVideoID(videoID)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment