Skip to content

Instantly share code, notes, and snippets.

@steventroughtonsmith
Created February 2, 2024 17:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steventroughtonsmith/279a8596e37f269657554156d9a37075 to your computer and use it in GitHub Desktop.
Save steventroughtonsmith/279a8596e37f269657554156d9a37075 to your computer and use it in GitHub Desktop.
UIKit proxy for visionOS 3D transforms and effects
//
// V3DViewContainer.swift
// Vision3DUIKit
//
// Created by Steven Troughton-Smith on 02/02/2024.
//
import UIKit
import SwiftUI
struct _V3DViewWrapper: UIViewRepresentable {
var view:UIView
func makeUIView(context: Context) -> some UIView {
return view
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
}
struct V3DViewInternal: View {
var view:UIView
@ObservedObject var parent:V3DViewContainer
var body: some View {
if parent.realityGlassEnabled {
_V3DViewWrapper(view: view)
.glassBackgroundEffect(in: .rect(cornerRadius: parent.realityGlassCornerRadius))
.rotation3DEffect(parent.realityRotation)
.transform3DEffect(parent.realityTransform)
.frame(depth: parent.realityDepth)
.animation(.spring)
}
else {
_V3DViewWrapper(view: view)
.rotation3DEffect(parent.realityRotation)
.transform3DEffect(parent.realityTransform)
.frame(depth: parent.realityDepth)
.animation(.spring)
}
}
}
class V3DViewContainer: UIView, ObservableObject {
@Published var realityRotation:Rotation3D = .identity
@Published var realityTransform:AffineTransform3D = .identity
@Published var realityDepth = 0.0
@Published var realityGlassEnabled = false
@Published var realityGlassCornerRadius = CGFloat.zero
let contentView:UIView
var hostingController:UIHostingController<V3DViewInternal>? = nil
init(contentView:UIView) {
self.contentView = contentView
super.init(frame: .zero)
hostingController = UIHostingController(rootView: V3DViewInternal(view: contentView, parent:self))
if let hostingController {
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(hostingController.view)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment