我有一个小按钮可以显示和隐藏UI元素。当我按下主屏幕按钮离开应用程序然后返回到该应用程序时,该按钮跳至其原始位置,而不是其动画设置的新位置。
所需的操作是将其保留在用户离开并回到应用程序时的位置。
@IBAction func hideChartTapped(_ sender: Any) {
let delay = 0.3
if chart.isHidden {
self.chart.isHidden = false
UIView.animate(withDuration: delay, animations: {
self.hideChart.center.y = (self.hideChart.center.y - self.chart.bounds.height)
self.chart.center.y = (self.chart.center.y - self.chart.bounds.height)
}, completion: nil)
} else {
UIView.animate(withDuration: delay, animations: {
self.hideChart.center.y = (self.hideChart.center.y + self.chart.bounds.height)
self.chart.center.y = (self.chart.center.y + self.chart.bounds.height)
self.hideChart.setNeedsDisplay()
}, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
self.chart.isHidden = true
})
}
}
这是我用来隐藏和显示图表以及移动按钮的代码。按钮和图表的默认位置是在主故事板中设置的。
答案 0 :(得分:0)
我知道了。我也没有更新约束。我仍在对中心运动进行动画处理,但是在动画之后,我将重置约束,如下所示:
@IBAction func hideChartTapped(_ sender: Any) {
let delay = 0.3
if chart.isHidden {
self.chart.isHidden = false
UIView.animateKeyframes(withDuration: delay, delay: 0, options: [], animations: {
self.hideChart.center.y = (self.hideChart.center.y - self.chart.bounds.height)
self.chart.center.y = (self.chart.center.y - self.chart.bounds.height)
}, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
self.hideChartBottomConstraint.constant = 0
self.hideChart.layoutIfNeeded()
})
} else {
UIView.animateKeyframes(withDuration: delay, delay: 0, options: [], animations: {
self.hideChart.center.y = (self.hideChart.center.y + self.chart.bounds.height)
self.chart.center.y = (self.chart.center.y + self.chart.bounds.height)
}, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
self.hideChartBottomConstraint.constant = self.chart.bounds.height
self.hideChart.layoutIfNeeded()
self.chart.isHidden = true
})
}
}