Add UIView (from xib) with transparency to SceneKit

孤街浪徒 提交于 2019-12-10 13:19:50

问题


I'm trying to load a UIView into SceneKit with a translucent background, but currently, it just fades to white as I decrease the opacity. I have a very simple UIView layout in a .xib file that I want to load into SceneKit. So far I can display the UIView in the SCNMaterial, change any text fields, images, etc inside the view without a problem. However, I cannot let it have transparency. If I change the alpha of the view it just fades to white.

most of the code is the below:

if let cardView = Bundle.main.loadNibNamed("OverlayCard", owner: self, options: nil)?.first as? OverlayCard {
  cardView.backgroundColor = UIColor(displayP3Red: 1.0, green: 0.4, blue: 0.9, alpha: 0.2)
  let newplane = SCNPlane()
  let newMaterial = SCNMaterial()
  cardView.alpha = 0.2
  cardView.isOpaque = false
  newMaterial.diffuse.contents = cardView
  newMaterial.blendMode = .add
  newplane.materials = [newMaterial]
  let viewNode = SCNNode(geometry: newplane)
  self.addChildNode(viewNode)
}

I've left in various things like assigning the blendMode, backgroundColor with 0.2 opacity as well as the entire view because these are all things I've tried but still get a white background with some of the view's elements on top of the white very faded out. Have also tried blendMode = .alpha to find no difference. 'self' here is a subclass of SCNNode.

Does anyone know how I can make this view's background fade to transparent, rather than fade to white? Or, another way to load a view into SceneKit.


回答1:


Try setting the layer of the view as diffuse.contents instead of the view itself.

newMaterial.diffuse.contents = cardView.layer


来源:https://stackoverflow.com/questions/47421392/add-uiview-from-xib-with-transparency-to-scenekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!