0
votes

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.

2
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

2 Answers

0
votes
  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
        })
    }
}
0
votes

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.