Ios 单击textField后按钮移动

Ios 单击textField后按钮移动,ios,swift,animation,uitextfield,uitextfielddelegate,Ios,Swift,Animation,Uitextfield,Uitextfielddelegate,我在一个登录屏幕上工作,你首先选择是通过电子邮件、FB还是Twitter注册。如果选择电子邮件,按钮将滑出屏幕,电子邮件登录、电子邮件文本字段、密码文本字段和登录按钮将滑入屏幕。我使用以下代码: @IBAction func emailLoginButtonPressed(sender: UIButton) { UIView.animateWithDuration(1, animations: { () -> Void in self.facebookButton

我在一个登录屏幕上工作,你首先选择是通过电子邮件、FB还是Twitter注册。如果选择电子邮件,按钮将滑出屏幕,电子邮件登录、电子邮件文本字段、密码文本字段和登录按钮将滑入屏幕。我使用以下代码:

@IBAction func emailLoginButtonPressed(sender: UIButton) {

    UIView.animateWithDuration(1, animations: { () -> Void in
        self.facebookButton.center = CGPointMake(self.facebookButton.center.x - 500, self.facebookButton.center.y)
        self.twitterButton.center = CGPointMake(self.twitterButton.center.x - 500, self.twitterButton.center.y)
        self.emailButton.center = CGPointMake(self.emailButton.center.x - 500, self.emailButton.center.y)
        self.emailTextField.center = CGPointMake(self.emailTextField.center.x - 500, self.emailTextField.center.y)
        self.passwordTextField.center = CGPointMake(self.passwordTextField.center.x - 500, self.passwordTextField.center.y)
        self.loginButton.center = CGPointMake(self.loginButton.center.x - 500, self.loginButton.center.y)

    })

}
这非常有效:当选择电子邮件登录时,所有内容都会滑到左边,您可以输入您的电子邮件和密码。但是,只要我在电子邮件文本字段或密码文本字段中单击,一切都会立即恢复正常。奇怪的是,如果在这一点上我再次选择电子邮件登录,它仍然有效,一切都会消失。但当我点击一个文本字段时,一切都会恢复,就好像我刚刚启动了这个应用程序一样

有什么想法吗?我想这可能与UITextFieldDelegate在检测到点击时做些什么有关,但我不知道

谢谢

添加

        self.view.translatesAutoresizingMaskIntoConstraints = true
        self.emailTextField.translatesAutoresizingMaskIntoConstraints = true
        self.passwordTextField.translatesAutoresizingMaskIntoConstraints = true
        self.emailButton.translatesAutoresizingMaskIntoConstraints = true
        self.facebookButton.translatesAutoresizingMaskIntoConstraints = true
        self.twitterButton.translatesAutoresizingMaskIntoConstraints = true
        self.loginButton.translatesAutoresizingMaskIntoConstraints = true

到视图控制器的viewDidLoad

如果使用自动布局,则无法直接编辑视图的框架,需要通过编辑约束来移动它们。我没有使用自动布局,我只是将它们放置在情节提要中,但没有在任何级别添加约束。是否关闭情节提要的自动布局?或者你是不是让它开着,只是没有添加约束?我不知道如何检查,但我假设它是开着的,因为我将它用于其他视图控制器。不幸的是,Xcode告诉我这段代码不正确,唯一接近它的是TranslatesAutoResizengMaskintoConstraints()但这并不能改变我的问题。如果您使用的是swift 1.0,您应该进行更新,但同时使用SetTranslatesAutoResizezingMaskintoConstraints(true)。如果您需要更多信息,可以在此处阅读: