David Mempin
David Mempin

Reputation: 55

UIView.animate not animating on one function

I have two functions that animates a UIView. One is for showing it and the other is for dismissing the view. What's weird is the animation for showing the view is working but the one for dismissing isn't.

show() is being called right after the view has been added as a subview.

func show() {
    self.viewContainer.frame.origin.y = self.view.frame.height + 500
    UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
        self.viewContainer.frame.origin.y = self.view.frame.height
    }, completion: nil)
}

While dismiss() is called using a UITapGestureRecognizer or a button press.

func dismiss() {
    self.overlay.isHidden = true
    self.viewContainer.frame.origin.y = self.view.frame.height
    UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
        self.viewContainer.frame.origin.y = self.view.frame.height + 500
    }) { (_) in
        self.view.removeFromSuperview()
    }
}

What seems to be the problem here?

Upvotes: 0

Views: 957

Answers (3)

David Mempin
David Mempin

Reputation: 55

Fixed it by animating the bottom constraint of the view instead of its frame.

UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
    self.viewBottomConstraint.constant = -500
    self.view.layoutIfNeeded()
}) { (_) in
    self.view.removeFromSuperview()
}

Got a hint out of this: https://stackoverflow.com/a/52378252/9629494

Upvotes: 0

sudayn
sudayn

Reputation: 1204

remove this code in dissmis()

self.viewContainer.frame.origin.y = self.view.frame.height

Why are you doing this in show()?

self.viewContainer.frame.origin.y = self.view.frame.height

If you wanted to show in center use

self.viewContainer.center.y = self.view.center.y

Upvotes: 0

Mohmmad S
Mohmmad S

Reputation: 5088

You can try this

func dismiss() {
 UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
     self.viewContainer.frame.origin.y = self.view.frame.height + 500
     self.overlay.isHidden = true
 }, completion: nil)
}

Upvotes: 1

Related Questions