如何给场景添加标签? (场景工具包)

问题描述 投票:0回答:1

我想向我的场景添加标签和按钮。我正在使用 SceneKit 框架。我创建了一个标签和按钮,但不知何故我的按钮可以工作,但我的标签没有出现。

我该怎么办?

这是我的代码;

func createSCNView(scene: SCNScene) {
    // retrieve the SCNView
    let scnView = self.view as! SCNView
    
    // set the scene to the view
    scnView.scene = scene
    
    // add a button to the scene
    let button = UIButton(type: .system)
    button.tintColor = UIColor.black
    button.titleLabel?.font = UIFont(name: "Arial", size: 25)
    button.setTitle("Back", for: .normal)
    button.sizeToFit()
    button.addTarget(self, action: #selector(didPressBack), for: .touchUpInside)
    button.center.x = 50
    button.frame.origin.y = 20
    scnView.addSubview(button)

    let expLabel = UILabel()
    expLabel.text = "Lütfen geliştirmek istediğiniz kas grubunu seçiniz"
    expLabel.textColor = .black
    expLabel.textAlignment = .center
    expLabel.lineBreakMode = .byWordWrapping
    expLabel.numberOfLines = 0
    expLabel.frame = .init(x: 20, y: self.view.frame.height - 60, width: 100, height: 25)
    expLabel.sizeToFit()
    expLabel.isEnabled = false
    scnView.addSubview(expLabel)
    
    // allows the user to manipulate the camera
    scnView.allowsCameraControl = true
    
    // show statistics such as fps and timing information
    scnView.showsStatistics = false
    
    // add a tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    scnView.addGestureRecognizer(tapGesture)
}
swift xcode label scenekit
1个回答
0
投票

代码很好。我必须调整标签框的 y 坐标才能使标签进入视图。另外,请记住启用标签显示。以下对我有用。

func setupScoreLabel(scene: SCNScene){
  let sceneView = self.view as! SCNView
  sceneView.scene = scene

  let scoreLabel = UILabel()
  scoreLabel.text = "Score: = 0"
  scorelabel.textColor = .black
  scoreLabel.textAlignment = .center
  scoreLabel.lineBreakMode = .byWordWrapping
  scoreLabel.numberOfLines = 0
  scoreLabel.frame = .init(x: 50, y: 70, width: 100, height: 20)
  scoreLabel.sizeToFit()
  scoreLabel.isEnabled = true
  sceneView.addSubView(scoreLabel)
}
© www.soinside.com 2019 - 2024. All rights reserved.