0

I've got a request to make a fidget spinner animation inside one shopping app, its meant to give the user a 'feel' before clicking buy so its a UIImageView that needs to be animated. I've added a custom single touch gesture recognizer that allows the user to spin/rotate the view, however the image now only rotates and stops as soon as the user lets go, what would be the best way to keep the 'velocity' going? Think of how something would be done with SpriteKit?

Sam Bing
  • 2,794
  • 1
  • 19
  • 29

1 Answers1

1

I left a comment regarding angular velocity and momentum but wanted to answer this question so the next person who needs help knows a way to do this.

Angular velocity is the speed of the spinning. What you want to do is apply an angular impulse which will start the spinning. The mass and angular momentum will slow it down as time passes, just like the actual physics behind the fidget spinners.

Add this to the touchesBegan method in the fidgetSpinner class

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    fidgetSpinner.physicsBody?.applyAngularImpulse(1)
}

Or add it to the scene but check that the touch was inside and on fidgetSpinner.

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first?.location(in: self)
    if fidgetSpinner?.contains(touch) {
        fidgetSpinner.physicsBody?.applyAngularImpulse(5)
    }
}
Alec O
  • 1,697
  • 1
  • 18
  • 31
  • This works on SpriteKit although my question was achieving the effect without SpriteKit, although the same applies. – Sam Bing May 26 '17 at 07:08
  • You had mentioned that my comment was helpful, and my comment spoke of angular velocity and momentum. Both are concepts in SpriteKit, and ones that may be helpful to people looking for a solution in the future. – Alec O May 26 '17 at 10:13
  • I didn't claim it wouldn't be, and your comment certainly was helpful, angular momentum and angular velocity were helpful as i emulated them without SpriteKit. Hence i also up-voted your answer. – Sam Bing May 26 '17 at 13:04