Title: Push animation with views
Tags: ios;swift;uikit
Question: I'm trying to create an animation where viewOne fills the screen and gets pushed off screen and replaced by viewTwo. My attempt below partially works - viewTwo is behaving like it's supposed to, but where viewOne is supposed to be there's just a white space.
import UIKit
class ViewController: UIViewController {
let viewOne = UIView()
let viewTwo = UIView()
func animate () {
view.sendSubview(toBack: viewOne)
let screenBounds = UIScreen.main.bounds
let finalToFrame = screenBounds
let finalFromFrame = finalToFrame.offsetBy(dx: 0, dy: -screenBounds.size.height)
viewOne.frame = finalToFrame.offsetBy(dx: 0, dy: +screenBounds.size.height)
UIView.animate(withDuration: 0.3, animations: {
self.viewOne.frame = finalToFrame
self.viewTwo.frame = finalFromFrame
}, completion: nil)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(viewOne)
viewOne.widthAnchor.constraint(equalTo: view.widthAnchor)
viewOne.heightAnchor.constraint(equalTo: view.heightAnchor)
viewOne.backgroundColor = UIColor.green
view.addSubview(viewTwo)
viewTwo.widthAnchor.constraint(equalTo: view.widthAnchor)
viewTwo.heightAnchor.constraint(equalTo: view.heightAnchor)
viewTwo.backgroundColor = UIColor.blue
view.sendSubview(toBack: viewTwo)
let swipe = UISwipeGestureRecognizer(target: self, action: #selector(animate))
swipe.direction = .down
view.addGestureRecognizer(swipe)
Comment: @matt I am constraining the width and height of the views, not the position which I'm trying to animate.

Comment: Please supply sufficient code that we can actually _run_ it and _see_ the problem in action.

Comment: At the moment the problem is that you are adding subviews and animating them all in one go. You can't animate a view until it is in the hierarchy, and a view is not in the hierarchy until _after_ the code that adds it has _finished_ running. I'm surprised you're not reporting a message in the console warning about this.

Comment: Another problem is that it makes no sense to constrain a view (with anchors) and then animate its frame to somewhere else, as the contraints will just bring it right back to where it was. Frame animation and constraints are opposites.

Comment: Then your constraints are invalid. You cannot constrain just the width and height; you must supply full constraints or samantharichards@example.net. (This might explain why you never see one of the views, if that's what happening.) You have a lot of rethinking to do here!



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:07/16/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift><uikit>', 'Title': 'Push animation with views', 'ViewCount': 68, 'id': 45132742, 'user_AboutMe': '<p>I do swift as a hobby, and i have had no professional guidance. I think I have managed well for being self-taught.</p>\n', 'user_AccountId': 10209133.0, 'user_DisplayName': 'Victor Apeland', 'user_Reputation': 100.0, 'user_Views': 27.0, 'user_id': 7536868.0}}