【发布时间】:2015-05-26 00:26:00
【问题描述】:
那里有几个回答的问题与我的相似,如果不一样的话,但是,它们似乎都没有解决我的困境。我有一个UIImageView,我用UILongPressGesture 在屏幕上移动,我通过改变水平和垂直constraints 来移动它。如果这个image 没有走一定的距离,我希望它动画回到原来的位置。我正在使用以下是我用来尝试执行此操作的代码示例:
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 和 location.y 在别处定义为用户点击的位置
translation.x和translation.y定义为用户在superview中的位置
不用说,平移 image 时一切正常。它拖动,然后在手势结束时停止。问题是,如果用户不将image 拖过某个位置,我希望image 动画回到其原始位置,而不是平滑过渡,它会立即回到其原始位置而不是动画。基于类似问题的其他答案,我正在设置约束,然后在animateWithDuration 中调用layoutIfNeeded。
autolayout 有什么我失踪了吗?
要让image 以动画方式回到其原始位置,我需要进行哪些更改?
【问题讨论】:
-
抱歉我的答案有误,我在这里做了快速研究和一些测试,我已经成功了,请再次查看我的答案,我会为你编辑。
-
您更新的答案应该可以工作,但是,由于某种原因,似乎任何时候更改
constraint时,UIImageView的位置都会在animation之外更新。下面的@matt 答案对我有用。
标签: ios swift animation autolayout