2015-09-14 37 views
0

現在我有,如果底部的文本框被竊聽否則,如果頂部的文本字段上,然後拍了拍我的屏幕沒有轉變,轉變整個屏幕了一個功能:UITapGestureRecognizer各國不工作

func keyboardWillShow(notification: NSNotification) { 
     var tapTopTextField: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: nil) 
     topTextField.addGestureRecognizer(tapTopTextField) 
     topTextField.userInteractionEnabled = true 
     if(tapTopTextField.state == .Began) { 
      // don't move screen 
     } 
     // then it is the bottomTextField that is tapped 
     else { 
      self.view.frame.origin.y -= getKeyboardHeight(notification) 
     } 

    } 

所以使用這段代碼,如果topTextField被點擊,而另一種方式使它不改變,我的屏幕仍然會移動,通過讓狀態爲.Possible,但是這樣做會禁止當點擊bottomTextField時屏幕向上移動。有沒有我在這裏失蹤的東西,還有一個更優雅的方式來做到這一點?

更新:所以我意識到,這是發生的原因是因爲當這個函數被調用的文本框已經被選擇,因而被重置爲.Possible狀態

回答

0

我走的方式是設計UI等等文本字段在鍵盤的最前面。如果某些東西需要在底部並且可以通過鍵盤覆蓋,我讓用戶處理滾動。

0

所以我想出了答案,在這裏!

func keyboardWillShow(notification: NSNotification) { 

      if(topTextField.isFirstResponder()) { 
       // don't move screen 
      } 
      // then it is the bottomTextField that is tapped 
      else { 
       self.view.frame.origin.y -= getKeyboardHeight(notification) 
      } 

     } 

通過使用isFirstResponder我可以檢查textField是否被選中!