ios - Scenekit - 我如何拖放

标签 ios objective-c iphone scenekit

使用下面显示的代码,我可以在我的场景中有一个球体。蓝色背景...我是 iOS 中的 sceneKit 新手。我想知道我们如何将该对象(拖动球体)移动到另一个位置(掉落)。

    override func viewDidLoad() {
    super.viewDidLoad()
    sceneView = SCNView(frame: self.view.frame)
    sceneView.scene = SCNScene()
    self.view.addSubview(sceneView)


    let groundGeometry = SCNFloor()
    groundGeometry.reflectivity = 0
    let groundMaterial = SCNMaterial()
    groundMaterial.diffuse.contents = UIColor.blueColor()
    groundGeometry.materials = [groundMaterial]
    ground = SCNNode(geometry: groundGeometry)

    let camera = SCNCamera()
    camera.zFar = 10000
    self.camera = SCNNode()
    self.camera.camera = camera
    self.camera.position = SCNVector3(x: -20, y: 15, 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(ground)
    sceneView.scene?.rootNode.addChildNode(light)

    sceneView.scene?.rootNode.addChildNode(sphere1)

}

最佳答案

Mee,几个月前我用 objective-c 尝试过 sceneKit,它可能可以帮助您概括解决方案。对于节点的拖放,我使用了 UIPanGestureRecognizer。

//用于平移(移动)的手势识别器

UIGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[[self view] addGestureRecognizer:panGestureRecognizer];

//然后在“handlePanFrom”中

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {


    if (recognizer.state == UIGestureRecognizerStateChanged)
    {
        //Getting the new location of the view after pan gesture
        CGPoint translation = [recognizer translationInView:recognizer.view];

        //Converting this translation as per the SpriteKit coordinates
        translation = CGPointMake(translation.x, -translation.y);

        //Changing the position of sphere node with the translation
        CGPoint position = [sphere position];
        [sphere setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)];

        //Resetting the values of recognizer
        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        float scrollDuration = 1;
        CGPoint velocity = [recognizer velocityInView:recognizer.view];

        //Taking current position of sphere node
        CGPoint pos = [sphere position];

        //Finding change in position on the basis of velocity and scroll duration
        CGPoint p = mult(velocity, scrollDuration);

        //Calculating new position of the sphere
        CGPoint newPos = CGPointMake(pos.x + p.x, pos.y - p.y);

        [sphere removeAllActions];

        //Action for moving the sprite to the new location
        SKAction *moveTo = [SKAction moveTo:newPos duration:scrollDuration];
        [moveTo setTimingMode:SKActionTimingEaseOut];
        [sphere runAction:moveTo];
    }
}

要获得更多帮助,您可以查看这个不错的教程 http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites

关于ios - Scenekit - 我如何拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31980582/

相关文章:

ios - 在运行时使用客户端 ID 控制的 Paypal

ios - __mh_execute_header 崩溃

objective-c - 为什么移动和旋转 NSView 会将其旋转中心更改为框架的原点?

ios - 连接两个文本字段,以便在一个文本字段中输入值会导致更改另一个文本字段的值

ios - 推送通知的证书和配置文件

ios - 当 iPad 旋转时,ActionSheet 的 PopoverController 不会停留在窗口的中心

ios - 从 Objective-C 初始化 Swift Storyboard

iphone - 扩展 UITextView

iphone - 如何调整 cell.textLabel 的大小?

ios - 如何获取保存在临时目录中的视频的网址