如何跟踪动画状态

时间:2017-10-11 04:49:51

标签: ios swift uiviewanimation

上下文 我在堆栈视图中有两个文本字段。当任何一个被轻敲时,我都需要它们同时向上动画。

当您从一个文本字段切换到另一个文本字段时,我希望它们都保留在原位。它应该只在键盘被解雇时动画回来。

由于两个文本字段都有自己的委托,因此从一个到另一个单击会触发向下动画,稍微会引起一点跳跃。跳转是我想删除的错误。

我尝试了什么: 我创建了一个Bool,如果已经发生动画,它应该跟踪。然而,通过自我附加更改其状态,以及使用方法触发它似乎不起作用。

func didAnimate() {
    self.isAnimated = true
    print("Didanimate result:" + String(isAnimated))
}

func animateUp() {
    // moves views up to avoid keyboard
    UIView.animate(withDuration: 0.5,
                   delay: 0,
                   options: .curveEaseOut,
                   animations: {
                    self.textFieldsY.constant += 200
                    //print("~~~~~~~~~")
                    self.view.layoutIfNeeded()
                    self.didAnimate()
                    print(self.isAnimated)
    }, completion: { (finished: Bool) in
        self.isAnimated = true
    })
}

func animateDown() {
    // moves views down after keyboard leaves
    UIView.animate(withDuration: 1,
                   delay: 0,
                   options: .curveEaseOut,
                   animations: {
                    self.textFieldsY.constant -= 200
                    self.view.layoutIfNeeded()
                    self.isAnimated = false
    }, completion: nil)
}

//MARK:- Text Field delegate
func textFieldDidBeginEditing(_ textField: UITextField) {
    if (keyBoardIsVisible) {
        // no need to animate anything
    } else {
        self.animateUp()
        self.keyBoardIsVisible = true
    }
    print("initial animate result:" + String((isAnimated)))

}
func textFieldDidEndEditing(_ textField: UITextField) {
    if(keyBoardIsVisible) {
    animateDown()
        self.keyBoardIsVisible = false
    }
}

1 个答案:

答案 0 :(得分:0)

请勿使用textFieldDidBeginEditingtextFieldDidEndEditing来处理键盘动画。因为无论何时切换到第二个文本域textFieldDidEndEditing都会被调用。

我认为更好的方法是使用本地通知在Swift 3中使用这两个通知来处理这种情况:

override func viewDidLoad() {
  super.viewDidLoad()

  NotificationCenter.default.addObserver(self, selector: #selector(YourController.keyboardWillShow(sender:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
  NotificationCenter.default.addObserver(self, selector: #selector(YourController.keyboardWillHide(sender:)),name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func animateUp() {
  // moves views up to avoid keyboard
  UIView.animate(withDuration: 0.5,
                 delay: 0,
                 options: .curveEaseOut,
                 animations: {
                  self.textFieldsY.constant += 200
                  //print("~~~~~~~~~")
                  self.view.layoutIfNeeded()
                  print(self.isAnimated)
  }, completion: { (finished: Bool) in
  })
}

func animateDown() {
  // moves views down after keyboard leaves
  UIView.animate(withDuration: 1,
                   delay: 0,
                   options: .curveEaseOut,
                   animations: {
                    self.textFieldsY.constant -= 200
                    self.view.layoutIfNeeded()
    }, completion: nil)
}


func keyboardWillShow (notification: NSNotification) {
  if (keyBoardIsVisible) {
    return
  } else {
      self.animateUp()
      self.keyBoardIsVisible = true
  }
  print("initial animate result:" + String((isAnimated)))

}

func keyboardWillHide (notification: NSNotification) {
  if(keyBoardIsVisible) {
    animateDown()
    self.keyBoardIsVisible = false
  }
}