2

There are several answered questions out there similar if not the same as mine, however, none of them seem to solve my dilemma. I have an UIImageView that I move across the screen with the UILongPressGesture that I move by changing the horizontal and vertical constraints. If this image does not go a certain distance, I want it to animate back to its original position. I am using the following is an example of the code I am using to try and do this:

if sender.state == UIGestureRecognizerState.Changed {

    image1ConstraintX.constant = imageConstX + (translation.x - location.x)
    image1ConstraintY.constant = imageConstY + (translation.y - location.y)

} else if sender.state == UIGestureRecognizerState.Ended {

    var xPosition = self.image1.frame.origin.x + image1.frame.width/2
    var yPosition = self.image1.frame.origin.y + image1.frame.width/2
    imageConstX = image1ConstraintX.constant
    imageConstY = image1ConstraintY.constant

    if xPosition < 215 && yPosition < 210 {
        self.image1ConstraintX.constant = 13
        self.image1ConstraintY.constant = 17
        UIView.animateWithDuration(1.3, delay: 0.0, options: nil, animations: {
                    self.view.layoutIfNeeded()

                    }, completion: nil)
            }

location.x and location.y are defined elsewhere as the location the user clicks

translation.x and translation.y are defined as the location the user is in the superview

Needless to say, everything works when panning the image. It drags, and then stops when the gesture ends. The problem is that if the user doesn't drag the image past a certain location, I want the image to animate back to its original location and instead of a smooth transition, it is immediately going to its original position and not animating at all. Based on the other answers to similar questions I am setting the constraints and then calling layoutIfNeeded in the animateWithDuration.

Is there something with autolayout I am missing?

What would I have to change to get the image to animate back to its original position?

matt
  • 447,615
  • 74
  • 748
  • 977
Ryguyu
  • 173
  • 14
  • sorry about the wrong answer I did a quick research and some test here and I have it working, have a look in my answer again I edit it for you. – Icaro May 26 '15 at 01:05
  • Your updated answer should work, however, for some reason it seems like anytime a `constraint` is changed the `UIImageView` position is updated outside of the `animation`. @matt answer below is working for me. – Ryguyu May 26 '15 at 01:25

1 Answers1

2

I have not tested this, but in these situations my first impulse is always to try stepping out with a delay to the main thread. So, first, paste into your file (after the import but before everything else) the delay utility that I give here. Now wrap your final case in a very short delay:

if xPosition < 215 && yPosition < 210 {
    delay(0.1) {
        self.image1ConstraintX.constant = 13
        self.image1ConstraintY.constant = 17
        UIView.animateWithDuration(1.3, delay: 0.0, options: nil, animations: {
            self.view.layoutIfNeeded()
        }, completion: nil)
    }
}

If that doesn't solve it, then my next course of action would be to think about these lines:

imageConstX = image1ConstraintX.constant
imageConstY = image1ConstraintY.constant

Those are not local variables, so what are they? Maybe setting them is having a side effect that you are not taking into account.

Community
  • 1
  • 1
matt
  • 447,615
  • 74
  • 748
  • 977