首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewAnimation带来的麻烦

UIViewAnimation带来的麻烦
EN

Stack Overflow用户
提问于 2021-03-03 09:53:49
回答 1查看 18关注 0票数 0

我在UIView动画上遇到麻烦了。当我点击其中一个UITextFields时,基本上一切正常,直到我在选择另一个UITextField之后选择了另一个UITextField,字段会上下移动,然后在我选择另一个UITextField后返回到相同的位置。

我做错了什么?我期望的是将包含我的字段的整个UIStackView移动到up中,以避免键盘覆盖所有内容。此外,为了在我单击另一个UITextField时保持动画静止,只是在键盘关闭时返回到默认位置。

代码语言:javascript
复制
class LoginViewController: UIViewController {
    
    var coordinator: MainCoordinator?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewTapped()
        setupScreen()
        setupViews()
        setConstraints()
    }
    
    private func setupScreen() {
        self.view.backgroundColor = .systemPink
    }
    
    private func setConstraints() {
        
        self.textFieldLogin.heightAnchor.constraint(equalToConstant: 50).isActive = true
        self.textFieldLogin.widthAnchor.constraint(equalToConstant: 190).isActive = true
        //
        self.textFieldSenha.heightAnchor.constraint(equalToConstant: 50).isActive = true
        self.textFieldSenha.widthAnchor.constraint(equalToConstant: 190).isActive = true
        //
        self.loginButton.widthAnchor.constraint(equalToConstant: 145).isActive = true
        self.loginButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
        //
        self.stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
        self.stackView.widthAnchor.constraint(equalToConstant: 200).isActive = true
        self.stackView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        self.stackView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        
        
    }
    
    private func setupViews() {
        self.view.addSubview(self.stackView)
        self.viewTapped()
    }
    
    private lazy var textFieldLogin: UITextField = {
        let textFieldLogin = UITextField()
        textFieldLogin.tag = 1
        textFieldLogin.translatesAutoresizingMaskIntoConstraints = false
        textFieldLogin.layer.cornerRadius = 3.7
        textFieldLogin.textAlignment = .center
        textFieldLogin.placeholder = "Usuário"
        textFieldLogin.backgroundColor = .white
        textFieldLogin.delegate = self
        
        return textFieldLogin
    }()
    
    private lazy var textFieldSenha: UITextField = {
        let textFieldSenha = UITextField()
        textFieldSenha.tag = 2
        textFieldSenha.translatesAutoresizingMaskIntoConstraints = false
        textFieldSenha.layer.cornerRadius = 3.7
        textFieldSenha.textAlignment = .center
        textFieldSenha.placeholder = "Senha"
        textFieldSenha.backgroundColor = .white
        textFieldSenha.delegate = self
        
        return textFieldSenha
    }()
    
    private lazy var loginButton: UIButton = {
        let loginButton = UIButton()
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.layer.cornerRadius = 3.8
        loginButton.titleLabel?.font = UIFont(name: "Arial", size: 19)
        loginButton.setTitle("Entrar", for: .normal)
        loginButton.setTitleColor(.systemGreen, for: .normal)
        loginButton.backgroundColor = .white
        
        return loginButton
    }()
    
    private lazy var stackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [self.textFieldLogin, self.textFieldSenha, self.loginButton])
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.backgroundColor = .systemBlue
        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        return stackView
    }()
    
    private func viewTapped() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        self.view.addGestureRecognizer(tapGesture)
    }
    
    @objc func handleTap(sender: UITapGestureRecognizer) {
        UIView.animate(withDuration: 1.1, delay: 0, usingSpringWithDamping: 5.1, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
            self.stackView.frame.origin.y = self.stackView.frame.origin.y + 130
            self.textFieldLogin.resignFirstResponder()
            self.textFieldSenha.resignFirstResponder()
        }, completion: nil)
    }
}

extension LoginViewController: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        UIView.animate(withDuration: 1.1, delay: 0, usingSpringWithDamping: 5.1, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
            self.stackView.frame.origin.y = self.stackView.frame.origin.y - 130
        }, completion: nil)
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        UIView.animate(withDuration: 1.1, delay: 0, usingSpringWithDamping: 5.1, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
            self.stackView.frame.origin.y = self.stackView.frame.origin.y + 130
        }, completion: nil)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
}
EN

回答 1

Stack Overflow用户

发布于 2021-03-03 10:42:04

我在textFieldDidBeginEditing中使用了以下代码,一切正常!谢谢你们所有人。

代码语言:javascript
复制
 UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded() //Think I forget this method
            self.stackView.frame.origin.y = self.stackView.frame.origin.y - 130
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66449639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档