1

I've a Sprite that animates via texture array, with spinLeft being increasing the array index, and spinRight decreasing the index, as shown in the answer to my other question here:

https://stackoverflow.com/a/44792902/6593818

The animation is working fine, but now I am trying to control the animation via a gesture recognizer or some other form of input.

In the referenced question, the spin function has an input for speed (the TimeInterval), so I just need an output from a gesture recognizer:

enter image description here

I want to figure out what to do in terms of getting the object to react to my finger in the same way as shown in this video: https://youtu.be/qjzeewpVN9o

The video is beyond just a basic UIPanGesture reading velocity, I think. At least, I'm not sure how to implement it.

This project is to showcase a friend's sculptures via photography.

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)
}
Corey F
  • 621
  • 4
  • 14
  • 1
    You need 3D models of each of the things you want to show. The spinning behavior is not that hard to make, just tuning the drag rate from where the user pressed down, to where he is dragging. – Pochi Jun 29 '17 at 00:50
  • +1 to @Pochi for addressing the 3-D piece. Another (rather small) piece - Brad Larson's iOS Molecule app. It's quite old (2009? I would have thought 2008) and written in Obj-C, but it tracks your touch and "rotates" it in 3-D. I'm sure there's better (Swift?) examples, but this was the first thing I thought of when I saw the YouTube link. https://github.com/BradLarson/Molecules –  Jun 29 '17 at 00:56
  • I can't use a 3-D model and rotate it. The images have to consist of frames captured with a digital camera. Does it still apply? – Corey F Jun 29 '17 at 00:58
  • 1
    Permission to reword this question? (I have edit powers) This question is too broad and could use focusing. The point of this is just to get a working gesture for flinging / detecting swipes / pans with velocity. I'm worried it will get downvoted / closed by mods – Fluidity Jun 29 '17 at 01:29
  • 1
    @CoreyF I think it can, It's just a bunch of images put together, when you slide on the screen you are just changing witch frame is being displayed. The array is cyclical so once you reach the Nth image, you just start back from 0. It doesn't seem that hard, just pretty time consuming. Why don't you try doing this with 10 images? Load them as "Sprites" so they are loaded instantly. And set the "sprite frame" based on the dragging. (Drag right you start looping through the array to the right, etc..) – Pochi Jun 29 '17 at 01:49
  • @Fluidity for sure! Definitely reword it ... if you have a better way to phrase it. Thanks for offering to do that! – Corey F Jun 29 '17 at 02:00
  • @Pochi I already got the image display algo working for him: https://stackoverflow.com/a/44792902/6593818 Now we are just working on getting a decent gesture recognizer to work as in the video. I have a basic Pan set up but trying to configure it may take a while .. considering just using `.touchesMoved` and doing some hard math. – Fluidity Jun 29 '17 at 02:42
  • @Fluidity it's not that uncommon, that's exactly what the UIScrollView does. It handles acceleration, decay, and other stuff. You might even pull it off by using a really big scrollview, and somehow make it loop at the edges. (aka infinitely scrolling uiscrollview) which has been asked many times here on SO. Just a heads up, the UISCROLLVIEW delegate is not very granular. If you need super high granularity you gotta intercept the property itself (and ignore the delegate did scroll one) – Pochi Jun 29 '17 at 03:30
  • @Pochi I"m totally lost, do the animation as a scrollview? Interesting. – Fluidity Jun 29 '17 at 03:31
  • @Fluidity no i mean, catch the dragging gesture through a scrollview, it already offers many of the things you wanna implement. After all it internally implements a pan gesture but is optimized to "feel fluid" – Pochi Jun 29 '17 at 04:27
  • dude where did you go? Didn't you still need help w ur projects? – Fluidity Jul 09 '17 at 11:33

0 Answers0