我正在创建一个带有一些向上动画的应用程序。 当我单击按钮时,标题文本向上移动,标签从底部需要在3秒后向下移动。请看以下示例图像
这是原创设计。一旦我点击按钮动画工作正常。但是,如果我点击相同的项目动画搞砸了。我尝试使用以下代码。
self.sub_item.layer.removeAllAnimations()
item.layer.removeAllAnimations()
self.item.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.bottomConstraint.constant = 0
self.sub_item.hidden = false
UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.bottomConstraint.constant = self.item.frame.height/3.0
self.item.contentEdgeInsets = UIEdgeInsetsMake(-self.item.frame.height/3.0, 0, 0, 0)
self.view.layoutIfNeeded()
}, completion: { finish in
UIView.animateWithDuration(0.25, delay: 4.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: {
self.bottomConstraint.constant = 0
self.nitem.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.view.layoutIfNeeded()
}, completion: {finish in
self.sub_item.hidden = true
})
})
注意:如果我的问题令人困惑,请询问。
答案 0 :(得分:0)
只需在按钮点按下删除所有动画时重置约束: -
//remove animation and reset constraints
self.sub_item.layer.removeAllAnimations()
item.layer.removeAllAnimations()
self.bottomConstraint.constant = 0
self.nitem.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.view.layoutIfNeeded()
self.view.setNeedsUpdateConstraints()
self.view.updateConstraintsIfNeeded()
self.sub_item.hidden = false
//start animation
UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: {
self.bottomConstraint.constant = self.item.frame.height/3.0
self.item.contentEdgeInsets = UIEdgeInsetsMake(-self.item.frame.height/3.0, 0, 0, 0)
self.view.layoutIfNeeded()
}, completion: { finish in
UIView.animateWithDuration(0.25, delay: 4.0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: {
self.bottomConstraint.constant = 0
self.nitem.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.view.layoutIfNeeded()
}, completion: {finish in
self.sub_item.hidden = true
})
})