2017-10-11 141 views
0

上下文 我在堆栈视图中有两个文本字段。当任何一个被挖掘时,我都需要它们同时向上动画。如何跟踪动画状态

当你从一个文本框切换到下一个,我希望他们都留在原地。只有当键盘被解散时它才应该回退。

由于这两个文本字段都有自己的代表,点击从一个到下一个触发向下稍微动画引起了一点跳跃。跳转是我想删除的错误。

我曾尝试: 我创建的是应该保持跟踪,如果已经发生了动画布尔。然而,改变自己的状态,并用方法触发它似乎并不奏效。

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 
    } 
} 
+0

请不要发布您的代码为图像。将实际文字复制并粘贴到您的问题中。图像难以阅读,无法搜索,且代码不能被引用或复制。 – rmaddy

+0

不要发布图像的屏幕截图。 – user3287355

+0

感谢您的反馈 – codechef

回答

0

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

我认为更好的方法将使用本地通知来处理使用这两个通知,这种情况下在斯威夫特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 
    } 
} 
+0

我不理解在这种情况下使用通知中心的好处。 – codechef

+0

如果我不使用代表作为触发器,我将如何知道将这些通知放在哪里? – codechef