0

When I use the 'UIPanGestureRecognizer' to move a 'UIImageView' object, I notice the 'center' attribute doesn't change. Why is this, am I doing something wrong? Here is the code:

func handlePanning1(recognizer: UIPanGestureRecognizer)
{
    var index: Int = recognizer.view!.tag - 1 // index in the arrays for this piece

    var newTranslation: CGPoint = recognizer.translationInView(pieces[index])

    recognizer.view?.transform = CGAffineTransformMakeTranslation(lastTranslations[index].x + newTranslation.x, lastTranslations[index].y + newTranslation.y)

    // THIS ALWAYS PRINTS OUT THE SAME WHILE I'M PANNING
    // AND IF I PAN MULTIPLE TIMES IN DIFFERENT DIRECTIONS (AKA IT NEVER CHANGES)
    print(Int(pieces[index].center.x))
    print("\n")

    if recognizer.state == UIGestureRecognizerState.Ended {
        lastTranslations[index].x += newTranslation.x
        lastTranslations[index].y += newTranslation.y
    }
}
Rich Episcopo
  • 499
  • 1
  • 5
  • 17

1 Answers1

0

You are applying a transform to the view, you are not actually moving it. Think about it as though it is in a certain place but when it gets rendered, there are instructions to skew how it is show. If you want the view to have a different position, then you have to change its center property instead of transforming it.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
  • Oh OK, that makes perfect sense. So while panning with a 'UIPanGestureRecognizer' is it better to apply a transformation or actually move it (by changing the 'center')? – Rich Episcopo Oct 31 '14 at 19:20
  • depends on what you are trying to accomplish. I have not looked into which is more efficient, I would assume actually moving it is but that is just an assumption. If you are relying on its center to update to the position though, just go ahead and update the center. – Kris Gellci Oct 31 '14 at 19:27