@@ -26,13 +26,6 @@ - (instancetype)init:(RCTBridge*)_bridge {
2626 super.translatesAutoresizingMaskIntoConstraints = false ;
2727 super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB ;
2828 super.depthStencilPixelFormat = MTLPixelFormatDepth32Float ;
29-
30- xrView = [[MTKView alloc ] initWithFrame: self .bounds device: self .device];
31- xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
32- xrView.userInteractionEnabled = false ;
33- xrView.hidden = true ;
34- [self addSubview: xrView];
35- [BabylonNativeInterop updateXRView: xrView];
3629 }
3730 return self;
3831}
@@ -60,14 +53,26 @@ - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
6053
6154- (void )drawRect : (CGRect)rect {
6255 if ([BabylonNativeInterop isXRActive ]) {
63- xrView.hidden = false ;
64- } else {
65- xrView.hidden = true ;
56+ if (!xrView) {
57+ xrView = [[MTKView alloc ] initWithFrame: self .bounds device: self .device];
58+ xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
59+ xrView.userInteractionEnabled = false ;
60+ [self addSubview: xrView];
61+ [BabylonNativeInterop updateXRView: xrView];
62+ }
63+ } else if (xrView) {
64+ [BabylonNativeInterop updateXRView: nil ];
65+ [xrView removeFromSuperview ];
66+ xrView = nil ;
6667 }
6768
6869 [BabylonNativeInterop renderView ];
6970}
7071
72+ -(void )dealloc {
73+ [BabylonNativeInterop updateXRView: nil ];
74+ }
75+
7176- (void )takeSnapshot {
7277 // We must take the screenshot on the main thread otherwise we might fail to get a valid handle on the view's image.
7378 dispatch_async (dispatch_get_main_queue (), ^{
0 commit comments