2014-12-04 219 views
-1

在目標C中,這兩行簡單代碼用於向上滾動文本視圖。在SWIFT中是否有像這樣簡單的代碼來向上滾動文本字段?請幫助我。使用swift時,textfield在鍵盤出現時向上移動

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y); 
[scrollView setContentOffset:scrollPoint animated:YES]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 
[scrollView setContentOffset:CGPointZero animated:YES]; 
} 
+2

你是說你不能把它翻譯成Swift,而你想讓其他人爲你做? – matt 2014-12-04 15:33:12

+0

請澄清你的問題。你不知道如何在swift中編寫相同的代碼,或者它沒有按預期工作? – cmyr 2014-12-04 16:45:48

+0

大家好!是的,在目標c中,當鍵盤出現時,這兩行代碼將向上滾動。如何在SWIFT中翻譯此代碼? – 2014-12-06 06:18:00

回答

0

上:

UITextFieldTextDidEndEditingNotificationUITextFieldTextDidBeginEditingNotification

調用此方法壽動畫文本框

UIView.animateWithDuration(yourTime, delay: 0, options: .CurveEaseOut, animations: { 
yourTextField.frame = yourNewFrame 
}, completion: { finished in 
println("Basket doors opened!") 
}) 

希望這有助於!

+0

嗨..如何調用該方法? @ Arthur – 2015-02-06 07:40:06

+0

要調用的方法的名稱是來自UIVIew的「animateWithDuration」其類方法。 – 2017-04-07 03:15:15

0

其更好地利用這一點:你的文本字段的

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     scrollView.scrollEnabled = true 
    yourTextfield.addTarget(self, action: "textFieldShouldReturn:", forControlEvents: .EditingDidEndOnExit) 
    yourTextfield2.addTarget(self, action: "textFieldShouldReturn:", forControlEvents: .EditingDidEndOnExit) 
    ... 


    yourTextfield.tag = 1 
    yourTextfield2.tag = 2 
    ... 
} 

    func textFieldShouldReturn(textField: UITextField!) -> Bool { 
      let nextTag = textField.tag + 1 
      let nextResponder:UIResponder? = textField.superview?.viewWithTag(nextTag) 
      if ((nextResponder) != nil) { 
       nextResponder?.becomeFirstResponder() 
       var scrollPoint: CGPoint = CGPointMake(0, textField.frame.origin.y - 50) 
       scrollView.setContentOffset(scrollPoint, animated: true) 
      } else { 
       textField.resignFirstResponder() 
       scrollView.setContentOffset(CGPointZero, animated: true) 
      } 
      return false 
     } 
0

設置委託並實現以下方法

func textFieldDidBeginEditing(textField: UITextField) { 
     animateViewMoving(true, moveValue: 100) 
} 
func textFieldDidEndEditing(textField: UITextField) { 
     animateViewMoving(false, moveValue: 100) 
} 

func animateViewMoving (up:Bool, moveValue :CGFloat){ 
    var movementDuration:NSTimeInterval = 0.3 
    var movement:CGFloat = (up ? -moveValue : moveValue) 
    UIView.beginAnimations("animateView", context: nil) 
    UIView.setAnimationBeginsFromCurrentState(true) 
    UIView.setAnimationDuration(movementDuration) 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement) 
    UIView.commitAnimations() 
} 

參考:https://stackoverflow.com/a/32755167/988169

0

你可以寫這樣的代碼 -

let biginSel = #selector(LoginViewController.didBeginEditing(_:)) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: biginSel, name: UITextFieldTextDidBeginEditingNotification, object: usrNmFld) 
相關問題