SKNode scale from the touched point

前端 未结 4 1847
孤街浪徒
孤街浪徒 2020-12-09 06:26

I have added UIPinchGestureRecognizer to my scene.view to scale my content. I actually scale the parent node where all my visible contents reside. But I have problem though

4条回答
  •  旧时难觅i
    2020-12-09 06:41

    I have translated ninefifteen's solution for Swift and Pinch Gestures. I spent a couple days trying to get this to work on my own. Thank goodness for ninefifteen's Obj-C post! Here is the Swift version that appears to be working for me.

    func scaleExperiment(_ sender: UIPinchGestureRecognizer) {
    
    
        var anchorPoint = sender.location(in: sender.view)
    
        anchorPoint = self.convertPoint(fromView: anchorPoint)
    
        let anchorPointInMySkNode = _mySkNode.convert(anchorPoint, from: self)
    
        _mySkNode.setScale(_mySkNode.xScale * sender.scale)
    
        let mySkNodeAnchorPointInScene = self.convert(anchorPointInMySkNode, from: _mySkNode)
    
        let translationOfAnchorInScene = (x: anchorPoint.x - mySkNodeAnchorPointInScene.x, y: anchorPoint.y - mySkNodeAnchorPointInScene.y)
    
        _mySkNode.position = CGPoint(x: _mySkNode.position.x + translationOfAnchorInScene.x, y: _mySkNode.position.y + translationOfAnchorInScene.y)
    
        sender.scale = 1.0
    
    
    }
    

提交回复
热议问题