j3141592653589793238
j3141592653589793238

Reputation: 1894

How to create a slide animation inside a view controller?

I'd like to animate a view to slide in and out from the left.
What I did so far:

enter image description here

When the user clicks on the upper left icon, an action (show/hide menu-view) is triggered.

The "menu-view" includes the dark mask view, the semi-transparent white view and all three views (label + image).
Now this menu view shall slide in and out.

I tried to add a constraint to the menu view:

func viewDidLoad() {
    super.viewDidLoad()
    menuView.translatesAutoresizingMaskIntoConstraints = false
    menuViewLeftConstraint = NSLayoutConstraint(item: menuView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: -1000)
    menuViewLeftConstraint.isActive = true
}

and I toggled the constant on every click the user performs (-1000 or 0).

But the animation does not look like I thought it would.

Upvotes: 0

Views: 268

Answers (1)

J. Doe
J. Doe

Reputation: 13043

Why do this programmatically with a fixed constant? Set the right of your uiview (trailing) equal to the leading (left) of your superciew (uiviewcontroller). Create an outlet of that constraint and animate it by adding a constant which is equal to the uiview’s width and maybe some offset.

Alternative you can make your subview equal to your superviews width - someoffset, equal height -someoffset, centerX and centerY to the superview and animate the centerX constraint.

Upvotes: 3

Related Questions