1

I want to create the ability to spin a photographed object 360 degrees.

  • It spins endlessly based on the speed you "flick" .
  • You spin it left or right by flicking the object left or right .
  • You stop the spin when you touch to stop it if it's spinning.

enter image description here

Similar to the app The Elements by Theodore Grey.

Here's a video of the part of the app I'm trying to recreate. (i.e. the 3D spinner)

https://youtu.be/6T0hE0jGiYY

Here's a video of my finger interacting with it.

https://youtu.be/qjzeewpVN9o

I'm looking to use Swift and likely SpriteKit.

  1. How can I get from a real life object to something high quality and functional? I'm armed with a Mac , Nikon D810 and a green screen.

    I.e I'm guessing that a series of stop motion pictures is the way to go... but I'm feel that might not be fluid enough.

    For the purposes of this question I want to figure out what would make the most sense to program with. E.g. a video I'm rewinding and fast forwarding on command or a texture atlas of stop motion frames , etc.

    Note: Capturing software and photography techniques would be helpful information as I'm clueless in that department. But, I understand I can ask that on https://photo.stackexchange.com/ .


  1. What would the basic logic of my code be like for this object? In terms of:

A. The function setting up the object's animation or video or whatever is the best way to have the images prepared for use in my code.

B. The spin() function and

C. The stopSpin() function.

A whole project sample isn't needed (though I guess it'd be nice). But, those 3 functions would be enough to get me going.


  1. Is SpriteKit the wisest choice?
Corey F
  • 621
  • 4
  • 14
  • 1
    you can do this with SpriteKit... just make a texture atlas and you can play through the animation in forward or reverse. The flicking you can make your own "gesture recognizer" or you can implement UIGestureRecognizer and overlay it on spritekit. Making a sample project shouldn't be too dificult. I just need the assets. – Fluidity Jun 28 '17 at 01:52

1 Answers1

2

Here is the second draft of my answer that shows off the basic functionality of a simple sprite animation:

class GameScene: SKScene {

  // Left spin is ascending indices, right spin is descending indices.
  var initialTextures = [SKTexture]()

  // Reset then reload this from 0-6 with the correct image sequences from initialTextures:
  var nextTextures = [SKTexture]()

  var sprite = SKSpriteNode()

  // Use gesture recognizer or other means to set how fast the spin should be.
  var velocity = TimeInterval(0.1)

  enum Direction { case left, right }

  func spin(direction: Direction, timePerFrame: TimeInterval) {

    nextTextures = []
    for _ in 0...6 {

      var index = initialTextures.index(of: sprite.texture!)

      // Left is ascending, right is descending:
      switch direction {
      case .left:
        if index == (initialTextures.count - 1) { index = 0 } else { index! += 1 }
      case .right:
        if index == 0 { index = (initialTextures.count - 1) } else { index! -= 1 }
      }

      let nextTexture = initialTextures[index!]
      nextTextures.append(nextTexture)
      sprite.texture = nextTexture
    }

    let action = SKAction.repeatForever(.animate(with: nextTextures, timePerFrame: timePerFrame))
    sprite.run(action)
  }

  override func didMove(to view: SKView) {
    removeAllChildren()

    // Make our textures for spinning:
    for i in 0...6 {
      initialTextures.append(SKTexture(imageNamed: "img_\(i)"))
    }
    nextTextures = initialTextures

    sprite.texture = nextTextures.first!
    sprite.size = nextTextures.first!.size()
    addChild(sprite)
    spin(direction: .left, timePerFrame: 0.10)
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    spin(direction: .right, timePerFrame: velocity)
  }

  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    spin(direction: .left, timePerFrame: velocity)
  }
}

Right now you just click / release to alternate right left.

Todo for next draft:
- Implement gesture recognizer for velocity
- Implement decay if wanted (so it will slow down over time)

(Old video, new code does not reset frame to 0):

enter image description here

Image assets are found here for the animation: https://drive.google.com/open?id=0B3OoSBYuhlkgaGRtbERfbHVWb28

Fluidity
  • 3,985
  • 1
  • 13
  • 34
  • Awesome man! Thanks so much! This is exactly what I needed. I'm gonna wait for the next draft before I thoroughly review it all, but it makes a lot of sense on my first read. Reply to this comment when your final version is up and I'll accept that answer after I test it all out. Thanks for your hard work! :-) Though I'm wondering how to make it spin fast based on how fast you swipe the thing, instead of clicking it. ( I see that's in the next draft). Also , what would the stop() function look like that would stop the spin when you touch it while it's spinning? – Corey F Jun 28 '17 at 19:11
  • 1
    @CoreyF ios unfortunately doesn't have a "fling" gesture, so you could implement a basic PanGesture and then use the .velocity to control the `spinRight(timePerFrame: )` I'm working on making a fling gesture recognizer (something I've been wanting to do for a while) but I can't guarantee it will be amazing, or how long it will take. Honestly this is something that warrants a new question IMO. – Fluidity Jun 28 '17 at 20:27
  • Here's a video of my finger interacting with it... so you can see. Does your code still make sense? I think I mean swipe... I filmed my ipad and finger. What is the appropriate gesture? https://youtu.be/qjzeewpVN9o – Corey F Jun 28 '17 at 20:43
  • Also you'll notice. While the speed does decay... it never fully stops spinning. – Corey F Jun 28 '17 at 20:46
  • 1
    that's an impressive algorithm in that video, @CoreyF. You are definitely going to want to make a new question for that. My code above will work with whatever gesture recognizer you come up with.. the `spin` function has an input for speed (the TimeInterval), so you just need an output from a gesture recognizer. Could throw together a very hacky version of that but the one in the video is advanced – Fluidity Jun 28 '17 at 21:36
  • 1
    I went ahead and created a new question that deals with specifically asking about gestures. https://stackoverflow.com/questions/44814650/how-can-i-cause-my-object-to-spin-with-the-gestures-shown-in-this-video – Corey F Jun 29 '17 at 00:48
  • 1
    @CoreyF I will check it out. I'm working on a basic pan gesture recognizer that hopefully will work ok with this question too – Fluidity Jun 29 '17 at 00:56