0

How do I make subviews of stack interchange with animation

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

This works when views are not in stack view :

let lastXcoVar = lastView.center.x 

UIView.animateWithDuration(0.5, animations:
{
    lastView.center.x = nextView.center.x
    nextView.center.x = lastXcoVar
})
Sujay U N
  • 4,974
  • 11
  • 52
  • 88

1 Answers1

1

Animation is just a visualization. You can do the same animation as your views not in stack view, after the animation is finished we use exchangeSubviewAtIndex to update the views on stack view. For example:

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>)
}
dduyduong
  • 124
  • 1
  • 5