Swift StackView 与动画互换子视图

Swift StackView interchange subview with animation

如何制作带有动画的堆栈交换的子视图

UIView.animateWithDuration(0.5, animations:
{
    self.stackview.exchangeSubviewAtIndex(3, withSubviewAtIndex: 4)
})

这适用于视图不在堆栈视图中的情况:

let lastXcoVar = lastView.center.x 

UIView.animateWithDuration(0.5, animations:
{
    lastView.center.x = nextView.center.x
    nextView.center.x = lastXcoVar
})

动画只是一种可视化。您可以在不在堆栈视图中的视图中执行相同的动画,动画完成后我们使用 exchangeSubviewAtIndex 更新堆栈视图中的视图。例如:

let lastXcoVar = lastView.center.x

UIView.animateWithDuration(0.5, animations:
{
    lastView.center.x = nextView.center.x
    nextView.center.x = lastXcoVar
}) { _ in
    self.stackview.exchangeSubviewAtIndex(<index of lastView>, withSubviewAtIndex: <index of nextView>)
}