1

I want to perform some animation on secondTextField when the FirstTextfield becomesFirstResponder

After resign first responder the animation should reverse back in second text field. How can I achieve this in swift?

Note: It is different from moving textfield to the top on keyboard appears.

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        if(textField == firstTextField) {
    UIView.animateWithDuration(1.0, animations: {
        self.secTextField.frame = CGRectMake(self.secTextField.frame.origin.x + 500, self.secTextField.frame.origin.y, self.secTextField.frame.size.width, self.secTextField.frame.size.height) })
    }
    return true
}

What I want to do is, when the user types in firstTextField, the secTextField should go and hide out of sight. What actually happens is the secTextField comes from out of sight to its original position

AAA
  • 1,957
  • 4
  • 23
  • 42

3 Answers3

1

You can use the delegate methods of UITextField to achieve this. If you explain more about your animation I can help you with that also.

func textFieldDidBeginEditing(textField: UITextField) {

    if textField == self.firstTextField {
        UIView.animateWithDuration(2, delay: 0, options: nil, animations: { () -> Void in
            self.secondTextField.frame = CGRectOffset(self.secondTextField.frame, 500, 0)
            }, completion: nil)
    }
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    if textField == self.firstTextField {
        UIView.animateWithDuration(2, delay: 0, options: nil, animations: { () -> Void in
            self.secondTextField.frame = CGRectOffset(self.secondTextField.frame, -500, 0)
            }, completion: nil)
    }
    textField.resignFirstResponder()
    return true
}

Don't forget to set delegates on the UITextField objects (on your viewDidLoad)

self.firstTextField.delegate = self
self.secondTextField.delegate = self

You can download the example from here: https://github.com/alextarrago/text-field-test

Alex Tarragó
  • 956
  • 8
  • 16
  • func textFieldShouldBeginEditing(textField: UITextField) -> Bool { if(textField == firstTextField) { UIView.animateWithDuration(1.0, animations: { self.secTextField.frame = CGRectMake(self.secTextField.frame.origin.x + 500, self.secTextField.frame.origin.y, self.secTextField.frame.size.width, self.secTextField.frame.size.height)}) } return true } i want to move the secTextField out of my view, but what actually happens is it come from out of sight to its original position – AAA Jul 28 '15 at 08:03
  • Edit your previous question if you want to add more code, the comments are character limited! – Alex Tarragó Jul 28 '15 at 08:05
  • could u understand my issue here? – AAA Jul 28 '15 at 08:21
  • I've edited my answer with the working code (w/o using autolayout). – Alex Tarragó Jul 28 '15 at 08:26
  • No luck :( textFieldShouldReturn is working good. But textFieldDidBeginEditing animates as the secTextfield comes from out of sight to the original position – AAA Jul 28 '15 at 08:35
  • Mmhh... did you check the GitHub sample project? https://github.com/alextarrago/text-field-test – Alex Tarragó Jul 28 '15 at 08:39
  • Wow that is what I want still it is not working for me. I am using xib... will that be any problem? – AAA Jul 28 '15 at 08:44
  • Are you using auto layout? – Alex Tarragó Jul 28 '15 at 08:48
  • I'm out the office right now, I'll take a look at it later. If you want, send me a sample of your project so I can take a closer look at it. – Alex Tarragó Jul 28 '15 at 08:52
  • I am obviously missing something here. I just replicate the github example app and i faced the same issue. instead of the second text field hides, it comes from out of sight to its original position. I set the delegate and reference outlets. – AAA Jul 28 '15 at 08:59
  • You can reach me at alex@dribba.com – Alex Tarragó Jul 28 '15 at 09:14
1

In my case it works for me. Just use self.view.layoutIfNeeded()

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    if(textField == firstTextField) {
        UIView.animateWithDuration(1.0, animations: {
            self.secTextField.frame = CGRectMake(self.secTextField.frame.origin.x + 500, self.secTextField.frame.origin.y, self.secTextField.frame.size.width, self.secTextField.frame.size.height)
            self.view.layoutIfNeeded()
        })
    }
    return true
}
Litle Dev
  • 474
  • 1
  • 5
  • 18
0

I finally found the answer. I did the following steps.

  1. Clean My project,
  2. Enable and then again disable the Auto-Layouts.
  3. Set the delegate for those textfields both in xib and swift file.
  4. Then the coding:

         `self.secTextField.frame.origin.x = self.secTextField.frame.origin.x + 500`
    
AAA
  • 1,957
  • 4
  • 23
  • 42