What I'm looking to do here is to spin a SKSpriteNode around its anchor point and have its speed and direction match a pan gesture. So if my pan gesture is clockwise around the sprite then then sprite spins clockwise.
The problem I have with my code is that it works great for pans below the sprite from left to right/right to left, but not at all when I try and pan vertically and it makes the sprite spin the wrong way if I pan above the sprite.
Here's what I've got so far -
let windmill = SKSpriteNode(imageNamed: "Windmill")
override func didMoveToView(view: SKView) {
/* Setup gesture recognizers */
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
self.view?.addGestureRecognizer(panGestureRecognizer)
windmill.physicsBody = SKPhysicsBody(circleOfRadius: windmill.size.width)
windmill.physicsBody?.affectedByGravity = false
windmill.name = "Windmill"
windmill.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
self.addChild(windmill)
}
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
if (recognizer.state == UIGestureRecognizerState.Changed)
{
pinwheel.physicsBody?.applyAngularImpulse(recognizer.velocityInView(self.view).x)
}
}
I know the reason it's not spinning with vertical pans in that I'm only getting the x value so I figure I need to combine these somehow.
I have also tried this using applyImpulse:atPoint:, but that results in the whole sprite being swept away.
The following steps will rotate a node based on a pan gesture:
Here's an example of how to do that in Swift...
Declare pan handler method