I'm relatively new to iOS development and I'm currently working on the signup/login screen for my app. I want one of the buttons to animate up once it's clicked, change its function and in this way, have a button 'get a new role' once it's clicked. The problem is that once I animate the button (via transfrom) upwards, it stops being clickable. Once I animate it back to its original position, it becomes clickable again.
It seems like as if the button is visible but not clickable...
(the button I'm talking about is: loginButton
Here's my code:
class FirstViewController: UIViewController {
let loginButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("I already have an account ;)", for: .normal)
button.backgroundColor = UIColor.rgb(red: 241, green: 245, blue: 249)
button.layer.cornerRadius = 5
button.titleLabel?.font = UIFont(name: "AvenirNext-Medium", size: 17)
button.tintColor = UIColor.rgb(red: 123, green: 123, blue: 123)
button.addTarget(self, action: #selector(handleLoginshow), for: .touchUpInside)
button.isEnabled = true
return button
}()
@objc func handleLoginshow() {
// Animations triggered by LOGIN BUTTON
UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.4, options: .curveEaseOut, animations: {
self.perspectiveImageContainerView.transform = CGAffineTransform(translationX: 0, y: -30)
self.perspectiveImageContainerView.alpha = 0
self.closeButton.alpha = 1
self.closeButton.isEnabled = true
self.signupButton.transform = CGAffineTransform(translationX: 0, y: -25)
self.signupButton.alpha = 0
self.introTextLabel.transform = CGAffineTransform(translationX: 0, y: -30)
self.introTextLabel.alpha = 0
})
//
// Animations triggered by LOGIN BUTTON
UIView.animate(withDuration: 0.5, delay: 0.15, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.4, options: .curveEaseOut, animations: {
self.loginButton.transform = CGAffineTransform(translationX: 0, y: -70)
self.loginButton.setTitle("Login", for: .normal)
self.loginButton.setTitleColor(UIColor.rgb(red: 256, green: 256, blue: 256), for: .normal)
self.loginButton.backgroundColor = UIColor.rgb(red: 18, green: 211, blue: 99)
self.view.backgroundColor = UIColor.rgb(red: 241, green: 245, blue: 249)
self.logoContainerView.backgroundColor = UIColor.rgb(red: 241, green: 245, blue: 249)
self.emailTextField.alpha = 1
self.emailTextField.transform = CGAffineTransform(translationX: 0, y: 0)
self.passwordTextField.alpha = 1
self.passwordTextField.transform = CGAffineTransform(translationX: 0, y: 0)
})
self.loginButton.addTarget(self, action: #selector(handleLogin), for: .touchUpInside)
}
@objc func handleLogin() {
guard let email = emailTextField.text else { return }
guard let password = passwordTextField.text else { return }
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, err) in
if let err = err {
print("Failed to sign in with email:", err)
return
}
print("Successfully logged back in with user:", user?.uid ?? "")
guard let mainTabBarController = UIApplication.shared.keyWindow?.rootViewController as? MainTabBarController else { return }
mainTabBarController.setupViewControllers()
self.dismiss(animated: true, completion: nil)
})
}
fileprivate func setupInputFields() {
let stackView = UIStackView(arrangedSubviews: [signupButton, loginButton])
stackView.axis = .vertical
stackView.spacing = 10
stackView.distribution = .fillEqually
view.addSubview(stackView)
stackView.anchor(top: introTextLabel.bottomAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 78, paddingLeft: 30, paddingBottom: 0, paddingRight: 30, width: 0, height: 110)
}
}