Francesc Francesc - 1 month ago 21
iOS Question

Slide up animation in Swift

How can I animate a stack view to slide up starting from

x=0 up to y=500
, I have the following method in the
viewDidLoad()
which does a growing effect.

StackView.transform = CGAffineTransformMakeScale(0.0, 0.0)


And then I added a growing effect in the
viewDidAppear()
method

UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
self.StackView.transform = CGAffineTransformIdentity
}, completion: nil)


After the viewDidLoad method executes, the stack view is minimized. When the viewDidLoad method completes, the viewDidAppear method is invoked, and the animation begins and the stack view begins to grow. The animation stops when the stack view reaches it's original size.

Although is a nice effect that's not what I want to accomplish, I want the animation to slide up from
x = 0 and stops at y = 500
I tried to add the following code in the viewDidLoad to accomplish this effect, but I still get the same growing effect. Any suggestions on how to accomplish this?

StackView.transform = CGAffineTransformMakeTranslation(0, 500)

Answer

You´re almost there just make a few changes

// These values depends on the positioning of your element
let left = CGAffineTransformMakeTranslation(-300, 0)
let right = CGAffineTransformMakeTranslation(300, 0)
let top = CGAffineTransformMakeTranslation(0, -300)

UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
      // Add the transformation in this block
      // self.container is your view that you want to animate
      self.container.transform = top
}, completion: nil)