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?