我正在尝试使用平移手势实时缩放和SCNNode:
这是我目前的代码
let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(from:)))
sceneView.addGestureRecognizer(pinchGestureRecognizer)
@objc
func handlePinch(from recognizer: UIPinchGestureRecognizer){
var pinchScale = recognizer.scale
pinchScale = round(pinchScale * 1000) / 1000.0
sceneView.scene.rootNode.enumerateChildNodes { (node, stop) -> Void in
if(node.name == "Box01"){
node.scale = SCNVector3(x: pinchScale, y: pinchScale, z: pinchScale)
}
}
}
但是节点不会扩大或缩小?有人可以指出我的错误吗?
加载了SCNNode,并且像这样应用了动画,
sceneView.scene.rootNode.addChildNode(node)
loadAnimation(animation: .Attack, sceneName: "art.scnassets/attack", animationIdentifier: "attackID");
答案 0 :(得分:4)
让它在swift中工作
@objc func handlePinch(gesture: UIPinchGestureRecognizer){
if(scnnodeSelected){
if (gesture.state == .changed) {
let pinchScaleX = Float(gesture.scale) * tappedObjectNode.scale.x
let pinchScaleY = Float(gesture.scale) * tappedObjectNode.scale.y
let pinchScaleZ = Float(gesture.scale) * tappedObjectNode.scale.z
tappedObjectNode.scale = SCNVector3(pinchScaleX, pinchScaleY, pinchScaleZ)
gesture.scale=1
}
}
}
答案 1 :(得分:1)
我用它来处理比例:
@objc
func handlePinch(from recognizer: UIPinchGestureRecognizer){
var pinchScale = round(recognizer.scale * 1000)/1000000
let node_arm = sceneView.scene.rootNode.childNode(withName: "army", recursively: true)
node_arm?.runAction(.customAction(duration: 0, action: { node, progress in
node.physicsBody = nil
node.scale = SCNVector3(x: Float(pinchScale), y: Float(pinchScale), z: Float(pinchScale))
}))
}
答案 2 :(得分:1)
我发现这很有效。我也使用两个触摸来尝试检测我是否捏住一个对象,这样你就可以用任何一个手指抓住SCNNode。
注意,重置比例的线是必要的,否则它的行为确实很麻烦。我不能特别说出为什么会这样。
-(void)scaleObject:(UIPinchGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
CGPoint tapPoint = [recognizer locationOfTouch:1 inView:_sceneView]; //Get tap location on the screen from the 2nd touch
NSArray <SCNHitTestResult *> *result = [self.sceneView hitTest:tapPoint options:nil]; //Get result array, checks on if we hit a SceneNode or not
if ([result count] == 0) { //If the first touch doesn't grap the SceneNode, try the second touch
tapPoint = [recognizer locationOfTouch:0 inView:_sceneView];
result = [self.sceneView hitTest:tapPoint options:nil]; // Get the results
if ([result count] == 0) {
return; //No objects found, return
}
}
SCNHitTestResult *hitResult = [result firstObject]; //Get the first hitResult
scaledObject = [[hitResult node] parentNode];
if (scaledObject) {
[NotificationView showNotificationWithText:@"Object has been selected for pinch"];
}
}
if (recognizer.state == UIGestureRecognizerStateChanged) { //When pinch status is changing
if (scaledObject) { //If we have an object grabbed
CGFloat pinchScaleX = recognizer.scale * scaledObject.scale.x;
CGFloat pinchScaleY = recognizer.scale * scaledObject.scale.y;
CGFloat pinchScaleZ = recognizer.scale * scaledObject.scale.z;
[scaledObject setScale:SCNVector3Make(pinchScaleX, pinchScaleY, pinchScaleZ)];
}
recognizer.scale = 1; //Reset the scale, skipping this line causes really weird behavior
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"Done pinching");
scaledObject = nil; //Make sure that no object is set to scaledObject
}
}
希望这有帮助
答案 3 :(得分:1)
func addPinchGestureToSceneView(){
let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.handlePitch(withGestureRecognizer:)))
sceneView.addGestureRecognizer(pinchGestureRecognizer)
}
@objc func handlePitch(withGestureRecognizer recognizer: UIPinchGestureRecognizer) {
let tapRecognizer = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapRecognizer)
guard let node = hitTestResults.first?.node else {
return
}
if (recognizer.state == .changed) {
let pinchScaleX = Float(recognizer.scale) * node.scale.x
let pinchScaleY = Float(recognizer.scale) * node.scale.y
let pinchScaleZ = Float(recognizer.scale) * node.scale.z
node.scale = SCNVector3(x: Float(pinchScaleX), y: Float(pinchScaleY), z: Float(pinchScaleZ))
recognizer.scale=1
}
}
答案 4 :(得分:0)
Swift 5的更新:
@objc func handlePinch(_ gesture: UIPinchGestureRecognizer) {
if (gesture.state == .changed) {
yourSKNode.xScale *= gesture.scale
yourSKNode.yScale *= gesture.scale
gesture.scale = 1
}
}
和我的didMove
函数:
override func didMove(to view: SKView) {
super.didMove(to: view)
setupNodes()
self.view!.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(_:))))
}