Hello, Here is my code below.
struct ImmersiveView: View {
@Environment(\.presentationMode) var presentationMode
@GestureState private var scale: CGFloat = 1.0
@State private var showMenu = false
var body: some View {
ZStack {
RealityView() { content in
// Get the video URL
//Create Entity for the video
let videoEntity = Entity()
//Search for video in paths
guard let url = Bundle.main.url(forResource: "harmandir-sahib-sarovar", withExtension: "mp4") else { fatalError("Video was not found!") }
//create a simple AVPlayer
let asset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer()
//create a videoMaterial
let material = VideoMaterial(avPlayer: player)
//Made a Sphere with the videoEntity and asign the videoMaterial to it
videoEntity.components.set(ModelComponent(mesh: .generateSphere(radius: 1E3), materials: [material]))
//adjust the properties of the videoEntity(Sphere) if needed
videoEntity.scale = .init(x: 1, y: 1, z: -1)
videoEntity.transform.translation += SIMD3<Float>(0.0, 10.0, 0.0)
let angle = Angle.degrees(90)
let rotation = simd_quatf(angle: Float(angle.radians), axis: .init(x: 0, y: 0, z: 0))
videoEntity.transform.rotation = rotation
//add VideoEntity to realityView
content.add(videoEntity)
//start the VideoPlayer
player.replaceCurrentItem(with: playerItem)
//set the actionAtItemEnd property to .none
player.actionAtItemEnd = .none
//subscribe to the notification and seek back to start
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in
player.seek(to: CMTime.zero)
player.play()
}
player.play()
}
Color.clear // An invisible view that covers the whole screen
.gesture(MagnificationGesture().updating($scale) { value, state, transaction in
state = value
}.onEnded { value in
showMenu.toggle()
})
}
.overlay(
Group {
if showMenu {
VideoView(dismissAction: {
presentationMode.wrappedValue.dismiss()
})
}
}
)
}
}
struct VideoView: View {
let dismissAction: () -> Void
var body: some View {
VStack {
Button("Back") {
dismissAction()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
.background(Color.white)
}
}
I am working on a way to pinch anywhere on the screen in the VisionOS simulator to open up a menu. Unfortunately the coding above isn't working. Can you please help me out. Thank you.