0

When I start typing in uitextfield i want some animation to happen and it should not reverse back until i finish typing. I am using this code to animate:

func textFieldDidBeginEditing(textField: UITextField) {
    println("begin")
    UIView.animateWithDuration(1.0, animations:{
        self.secondTextfield.frame = CGRectMake(self.secondTextfield.frame.origin.x + 500, self.secondTextfield.frame.origin.y, self.secondTextfield.frame.size.width, self.venueTextField.frame.size.height)
    })
}

What I want to do is: when i start typing in first textfield, i want the second text field to hide out from view and when i finish typing, i want it to animate back in.

What my issue is: When I start typing the animation happens and it comes back to the original position.It doesn't wait for me to finish typing.

AAA
  • 1,957
  • 4
  • 23
  • 42
  • Just played around with your code, it was interesting to see your problem. While I don't know the solution, here's a relevant tip: Since all you want to do is to move the textField's position, use `self.secondTextField.center.x += 500`. It's much easier on the eyes :] – Kelvin Lau Jul 28 '15 at 02:36

2 Answers2

0
  1. Why not change alpha to 0, intead change its frame?
  2. You need to identify witch textfield is editing.
  3. You need to deal with textFieldDidEndEditing to get second textField back.

Example:

func textFieldDidBeginEditing(textField: UITextField) {
    if textField == firstTextField {
        UIView.animateWithDuration(1.0, animations:{
            self.secondTextfield.alpha = 0
        })
    }
}

func textFieldDidEndEditing(textField: UITextField) {
    if textField == firstTextField {
        UIView.animateWithDuration(1.0, animations:{
            self.secondTextfield.alpha = 1
        })
    }
}
Klevison
  • 3,342
  • 2
  • 19
  • 32
  • I dont want to use alpha, i like to do the animation. In your answer the textfieldDidEndEditing works fine. But still when I enable the firstTextField, the animation occurs and comes back to the original position – AAA Jul 27 '15 at 23:52
  • check if `textFieldDidEndEditing` is called without you want. May in your code, you are resign (`resignFirstResponder `) `firstTextflied` focus and `textFieldDidEndEditing` is called. – Klevison Jul 28 '15 at 00:01
  • No, it is working fine, if I use alpha, it works good, but if I use animation, the issue occurs. – AAA Jul 28 '15 at 00:02
  • If i call that animation with a action of uibutton, the animation works fine, I face issue only with this textfieldsBeginEditing – AAA Jul 28 '15 at 00:04
  • Check if `textfieldsBeginEditing` is called just once and check use `finished` to change frame (http://stackoverflow.com/a/14670506/846780) – Klevison Jul 28 '15 at 00:06
  • yes it is calling once, i used println to find that. I dont understand 'finished' ?? – AAA Jul 28 '15 at 00:11
  • yes, I found that the completion is called when the animation ends along with the reverse back of animation. so the secondTextField hides and comes back and then only the completion is called – AAA Jul 28 '15 at 00:21
  • I think the animation is available only when the text BEGINs editing ... is there any way to call an action when the text is WHILE editing – AAA Jul 28 '15 at 00:30
  • https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/ – Klevison Jul 28 '15 at 00:34
  • `textField(_:shouldChangeCharactersInRange:replacementString:)` is called every time that you changed textFiled's value – Klevison Jul 28 '15 at 00:34
0

While not knowing exactly what the problem is, I've found a reasonable alternative to solving your problem:

Instead of animating your frame, animate the auto-layout constraint that defines your textField's x coordinate. For instance, the image below consists of two textFields. I want to animate the bottom one when I begin typing in the top one:

enter image description here

The vertical constraint is a centre x alignment that I want to animate. To achieve the effect, I connected an @IBOutlet to my NSLayoutConstraint:

enter image description here

I had my view controller adopt UITextFieldDelegate and implemented three methods:

extension ViewController: UITextFieldDelegate {
  func textFieldDidBeginEditing(textField: UITextField) {
    self.c.constant += 100 //c is my constraint
    UIView.animateWithDuration(1.0) {
      self.view.layoutIfNeeded()
    }
  }

  func textFieldDidEndEditing(textField: UITextField) {
    println("textFieldDidEndEditing")
    self.c.constant -= 100
    UIView.animateWithDuration(1.0) {
      self.view.layoutIfNeeded()
    }
  }

  func textFieldShouldReturn(textField: UITextField) -> Bool {
    a.resignFirstResponder()
    return true
  }
}

This method needs a bit of auto-layout know how, but I was able to achieve the effect you desired.

Kelvin Lau
  • 6,373
  • 6
  • 34
  • 57