Swift 如何使SCN视图小于屏幕大小?
视图控制器Swift 如何使SCN视图小于屏幕大小?,swift,scenekit,scnnode,sceneview,Swift,Scenekit,Scnnode,Sceneview,视图控制器 import UIKit import SceneKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let scnView = self.view as SCNView scnView.backgroundColor = UIColor.blackColor() l
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")
}
}
与任何其他
ui视图
一样,您可以设置SCNView的帧
(请参阅)
SCNScene
实例没有2D大小(您可以计算其根节点的边界框,但这有所不同)。只是阅读本文,不知道问题是否仍然存在,我正在一个小测试中运行该测试,似乎效果不错
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 scene=”行上收到一条错误消息。错误消息:calllet scene=Tile(frame:CGRect(x:0,y:0,宽度:100,高度:100))中的额外参数“frame”//也不起作用Tile
是SCNScene
的子类。场景没有frame
属性,也没有此类初始值设定项SCNView
does.ground可以添加到根场景。不需要抱歉,没有删除这些行/////let constraint=SCNLookAtConstraint(目标:地面)/////constraint.gimballocked=true