2017-10-11 112 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