Ryguyu
Ryguyu

Reputation: 173

Swift: Animating Constraints on UIImageView

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?

Upvotes: 2

Views: 980

Answers (1)

matt
matt

Reputation: 535944

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.

Upvotes: 2

Related Questions