Sourav Mishra
Sourav Mishra

Reputation: 531

Alpha is not working on labels and buttons in Swift

I have I'm trying to using fade in and fade out transition when any controller present and dismiss. For this, when new controller present I set alpha value of all the component/asset of the view like label, buttons etc to Zero in viewDidLoad and view didAppear I set back the alpha below to 1.

Below is the code I'm using:

override func viewDidLoad() {
        super.viewDidLoad()
        imageView1.isHidden = true
        imageView2.isHidden = true
        self.setupScreen()
        setAssetsAlphaZero()
    }
    
    private func setupScreen() {
        switch loginVM.loginScreenType {
        case .resetPassword:
            presentResetPasswordScreen()
        default: break
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
           super.viewDidAppear(animated)
           animateAssetsWithIdentity()
       }

 func animateAssetsWithIdentity() {
        self.view.layoutIfNeeded()
        self.view.setNeedsDisplay()
        hideShowAssets(hidden: false)
        UIView.animate(withDuration: 0.5, animations: { [weak self] in
            self?.passwordTextField.alpha = 1
            self?.forgotPasswordButton.alpha = 1
            self?.emailTextField.alpha = 1
            self?.loginButton.alpha = 1
            self?.loginLbl.alpha = 1
            self?.emailImg.alpha = 1
            self?.emailLbl.alpha = 1
            self?.passwordImg.alpha = 1
            self?.passwordLbl.alpha = 1
            self?.signUpBtn.alpha = 1
            self?.view.layoutIfNeeded()
            self?.view.setNeedsDisplay()
        }) {[weak self] (complete) in
            self?.signUpBtn.isEnabled = true
            self?.view.layoutIfNeeded()
            self?.view.setNeedsDisplay()
        }
        self.enableDisableButton()
    }
    
    func setAssetsAlphaZero() {
        self.view.layoutIfNeeded()
        self.view.setNeedsDisplay()
        UIView.animate(withDuration: 0.5, animations: {[weak self] in
            self?.passwordTextField.alpha = 0
            self?.forgotPasswordButton.alpha = 0
            self?.emailTextField.alpha = 0
            self?.loginButton.alpha = 0
            self?.loginLbl.alpha = 0
            self?.emailImg.alpha = 0
            self?.emailLbl.alpha = 0
            self?.passwordImg.alpha = 0
            self?.passwordLbl.alpha = 0
            self?.signUpBtn.alpha = 0
            self?.view.layoutIfNeeded()
            self?.view.setNeedsDisplay()
        }) {[weak self] (complete) in
            self?.hideShowAssets(hidden: true)
            self?.signUpBtn.isEnabled = false
            self?.view.layoutIfNeeded()
            self?.view.setNeedsDisplay()
        }
    }
    
    func hideShowAssets(hidden : Bool ) {
        self.passwordTextField.isHidden = hidden
        self.forgotPasswordButton.isHidden = hidden
        self.emailTextField.isHidden = hidden
        self.loginButton.isHidden = hidden
        self.loginLbl.isHidden = hidden
        self.emailImg.isHidden = hidden
        self.emailLbl.isHidden = hidden
        self.passwordImg.isHidden = hidden
        self.passwordLbl.isHidden = hidden
        self.signUpBtn.isHidden = hidden
    }

And when the controller dismiss than with help of delegate i notify the previous controller.

@IBAction func signUpButtonTapped(_ sender: UIButton) {
        setAssetsAlphaZero()
        self.delegate?.dissmissVC()
        delay(0.5) {[weak self] in
            self?.dismiss(animated: false, completion: nil)
        }
        
    }

To present the controller I used modalPresentationStyle = .overFullScreen because I need the present we controller should be transparent.

Upvotes: 0

Views: 402

Answers (1)

Daniel Wijono
Daniel Wijono

Reputation: 86

I don't think you need to call setNeedsDisplay. Because it will react to function drawRect as mention here. Also some suggestions, i think it's better to call setAssetsAlphaZero() then add delay 0.5 sec then call animateAssetsWithIdentity() . Hopefully this is helpful

When do I need to call setNeedsDisplay in iOS?

Upvotes: 0

Related Questions