0

This is iPad app by using SWIFT. It having two views. 1. starting_View 2. login_View These two views are in same ViewController. starting_View will be first view. By Clicking, NEXT button in first View, Starting View will move to left side by using animateDuration and same time, login_View will come from right. If we click Username/password fields (Any TextField), it will navigates to previous view.

Same time,,

if login_View will be first screen means, textField is working, keyboard is appearing. But in animateDuration, i couldn't type. Kindly help me. Am using XCODE 6.1.

Code (from comment):

@IBAction func getStart_button(sender: UIButton) {
    UIView.animateWithDuration(0.7, delay: 0.25, options: .CurveEaseOut, animations: {
        self.clt_login_vw.frame = CGRectMake(450, 56, 574, 660)  
    }, completion:nil)
}
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
Gopi Krish
  • 71
  • 1
  • 9
  • 1
    try after disabling autolayout on your ViewController. You can disable easily on Interface Builder. – bpolat Nov 17 '14 at 14:34
  • also show your code. – Woodstock Nov 17 '14 at 14:36
  • @IBAction func getStart_button(sender: UIButton) { UIView.animateWithDuration(0.7, delay: 0.25, options: .CurveEaseOut, animations: {self.clt_login_vw.frame = CGRectMake(450, 56, 574, 660) }, completion:nil) } – Gopi Krish Nov 17 '14 at 14:39
  • without auto layout, it is working fine. But if i need to add constraints means, how can i? @bpolat – Gopi Krish Nov 17 '14 at 14:59
  • hi @bpolat! any idea?? – Gopi Krish Nov 18 '14 at 09:31
  • Try to use this method discussed here: http://stackoverflow.com/questions/13296232/ios-how-does-one-animate-to-new-autolayout-constraint-height/26040569#26040569 – bpolat Nov 18 '14 at 10:35

1 Answers1

-1

In General try to never change frames directly when using Autolayout. The best way to move views have been for me to use CGAffineTransform.

let move = CGAffineTransformMakeTranslation(translation_X , translation_Y)

and inside your animation closure:

self.clt_login_vw.transform = move

Then to return it back just do inside the animation Closure:

self.clt_login_vw.transform = CGAffineTransformIdentity 

Resume:

let moveOut = CGAffineTransformMakeTranslation(translation_X , translation_Y)

@IBAction func getStart_button(sender: UIButton) { 
        UIView.animateWithDuration(0.7, delay: 0.25, options: .CurveEaseOut, animations: {
           [weak self] // Remember no not create cycles

            self!.clt_login_vw.transform = moveOut 

        }, completion:nil)
    }

Edit:

Im gonna add Noah Witherspoon's option. Another way to do this is getting the constraint that is holding your view horizontally, once you get it, you change its constant and call layoutIfNeeded() inside the closure:

@IBAction func getStart_button(sender: UIButton) { 

     constraint.constant = newConstant // enough to make it off the screen 

     UIView.animateWithDuration(0.7, delay: 0.25, options: .CurveEaseOut, animations: {
               [weak self] // Remember no not create cycles

                self!.view.layoutIfNeeded()

            }, completion:nil)
 }

The way to get the specific constraint, I'm more friend of making IBOutlets, so I get easily its reference in Code.

That is if you didn't make it by code.

  • Setting a transform is also the wrong way to go about this — to animate views using constraint-based layout, you should be changing the properties of the constraints. – Noah Witherspoon Nov 17 '14 at 20:26
  • I wouldn`t say is wrong, but an option, Anyway you are right that animating constraints constant changes would be the best – Себастьян Толедо Nov 17 '14 at 22:45
  • Hi all! My Query. If Controller is in AUTO LAYOUT, Textfield is working when we simply view gets loaded. But, if we moving the view using animate Duration, at that time TextField is not working. If we click the field, automatically navigates to previous view.. If controller is not in AUTO LAYOUT, Textfield is working when view gets loaded as well as in animateDuration. – Gopi Krish Nov 18 '14 at 05:02
  • Hey, as we said, the problem most probably is the way you are moving the view. To avoid autolayour problems, don't move frames directly, instead, change the auto layout constants, or use transformations to move the view as I tried to explain above. – Себастьян Толедо Nov 18 '14 at 15:17