How to properly get a gesture recognizer to fluidly spin a sprite according to velocity

五迷三道 提交于 2019-12-24 07:13:19

问题


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:

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)
}

来源:https://stackoverflow.com/questions/44814650/how-to-properly-get-a-gesture-recognizer-to-fluidly-spin-a-sprite-according-to-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!