3
votes

I have a UIView that I would like to animate off the screen and then bring another view into the screen. I'm using auto layout and I have a IBOutlet set up for the left and right NSLayoutConstraint. Its seem that with out a width set for the UIView that the view actually doesn't animate off the screen.

I'm trying to use this code to pus the UIView off:

  self.gameViewConstraintLeft.constant = -400
    UIView.animateWithDuration(1, animations: { () in
        self.view.layoutIfNeeded()
    })

    self.gameViewConstriantRight.constant = -400
    UIView.animateWithDuration(1, animations: { () in
        self.view.layoutIfNeeded()
    })

I have constraints set up on the top(0), left(0) and right(0). Maybe this is my issue that I'm pinning the view to the edge of the screen, but I want this to work on multiple screen sizes, so I'm just not sure the best method of achieving my goal.

1

1 Answers

4
votes

You are going to have problems moving the views if they're pinned on multiple sides horizontally or vertically.

I would suggest the following:

  1. Set up width, height, centerX, and centerY constraints for your two views. The width and height can be set to "Equal Widths" and "Equal Heights" with the superview to allow it to work with multiple screen sizes.

  2. Add an IBOutlets to the centerX constraint. Animate by modifying the constant property of this constraint:

    self.gameView1ConstraintCenterX.constant = -400
    UIView.animateWithDuration(1) {
        self.view.layoutIfNeeded()
    }
    
    self.gameView2ConstriantCenterX.constant = 0
    UIView.animateWithDuration(1) {
        self.view.layoutIfNeeded()
    }