【问题标题】:How do I make a scnView smaller than the screen size?如何使 scnView 小于屏幕尺寸?
【发布时间】:2015-02-09 08:14:48
【问题描述】:

视图控制器

import UIKit
import SceneKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scnView = self.view as SCNView

        scnView.backgroundColor = UIColor.blackColor()

        let sizeScnView = CGSize(width: 100.0, height: 100.0)
        let centerView = CGPoint(x: CGRectGetMidX(self.view.frame) - sizeScnView.width/2, y: CGRectGetMidY(self.view.frame) - sizeScnView.height/2)
        let scene = Tile(frame: CGRect(origin: centerView, size: sizeScnView))
        scnView.scene = scene
    }
}

平铺

import UIKit
import SceneKit

class Tile: SCNScene {

    override init() {
        super.init()

        let ball = SCNSphere(radius: 0.5)
        let ballNode = SCNNode(geometry: ball)
        ball.firstMaterial?.diffuse.contents = UIColor.orangeColor()
        self.rootNode.addChildNode(ballNode)

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

【问题讨论】:

  • 描述您当前的代码有什么问题,以及您想要什么不同的结果。 (一些看起来不像代码但应该像代码的行是否存在格式问题?)
  • 我在“let scene =”行收到一条错误消息。错误消息:调用中的额外参数“框架”
  • let scene = Tile(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) // 也不行
  • TileSCNScene 的子类。场景没有frame 属性,也没有这样的初始化程序。 SCNView 会。

标签: swift scenekit scnnode sceneview


【解决方案1】:

刚刚阅读这篇文章不知道问题是否仍然打开我在一个小测试中运行这个似乎工作正常

class ViewController: UIViewController {
   var sceneView: SCNView!
   var camera: SCNNode!
   var light: SCNNode!
   var sphere1: SCNNode!
   override func viewDidLoad() {
    super.viewDidLoad()
     let sizeScnView = CGSize(width: 420.0, height: 580.0)
     let centerView = CGPoint(x: CGRectGetMidX(self.view.frame) - sizeScnView.width/2, y: CGRectGetMidY(self.view.frame) - sizeScnView.height/2)
    sceneView = SCNView(frame: CGRect(origin: centerView, size: sizeScnView))
    sceneView.scene = SCNScene()
    self.view.addSubview(sceneView)
    sceneView.backgroundColor = UIColor.blackColor()
    let camera = SCNCamera()
    camera.zFar = 10000
    self.camera = SCNNode()
    self.camera.camera = camera
    self.camera.position = SCNVector3(x: -20, y: 55, z: 20)
    let constraint = SCNLookAtConstraint(target: ground)
    constraint.gimbalLockEnabled = true
    self.camera.constraints = [constraint]

    let ambientLight = SCNLight()
    ambientLight.color = UIColor.darkGrayColor()
    ambientLight.type = SCNLightTypeAmbient
    self.camera.light = ambientLight

    let spotLight = SCNLight()
    spotLight.type = SCNLightTypeSpot
    spotLight.castsShadow = true
    spotLight.spotInnerAngle = 70.0
    spotLight.spotOuterAngle = 90.0
    spotLight.zFar = 500
    light = SCNNode()
    light.light = spotLight
    light.position = SCNVector3(x: 0, y: 25, z: 25)
    light.constraints = [constraint]

    let sphereGeometry = SCNSphere(radius: 1.5)
    let sphereMaterial = SCNMaterial()
    sphereMaterial.diffuse.contents = UIColor.greenColor()
    sphereGeometry.materials = [sphereMaterial]
    sphere1 = SCNNode(geometry: sphereGeometry)
    sphere1.position = SCNVector3(x: -15, y: 1.5, z: 0)

   sceneView.scene?.rootNode.addChildNode(self.camera)
   sceneView.scene?.rootNode.addChildNode(light)
   sceneView.scene?.rootNode.addChildNode(sphere1)


   }
}

【讨论】:

  • 地面可以添加到根场景中。不需要很抱歉没有删除这些行 //////let constraint = SCNLookAtConstraint(target: ground) ///////// constraint.gimbalLockEnabled = true
【解决方案2】:

就像任何其他UIView 一样,您可以设置SCNViewframe(请参阅View Programming Guide for iOS)。

SCNScene 实例没有 2D 大小(您可以计算其根节点的边界框,但会有所不同)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 2023-03-24
    • 2012-03-27
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多