Buttons block animated as DropDown Menu

Good afternoon

I want to make a drop down menu animated. I succeed in unhiding the buttons, but can't succeed in make the drop down animation. I'm missing something. Some help please?

When I press botonDropDown, I want all the buttons to drop down.


Here is the code:


import UIKit

 extension UIView {
    
    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: frame.size.width, height: width)
        self.layer.addSublayer(border)
    }
    
    func addLeftBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }
    
    func addRightBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }
    
    func addTopBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
        self.layer.addSublayer(border)
    }
}


class ViewController: UIViewController {

    @IBOutlet weak var botonLong: UIButton!
    @IBOutlet weak var botonVolum: UIButton!
    @IBOutlet weak var botonMasa: UIButton!
    @IBOutlet weak var botonTemp: UIButton!
    @IBOutlet weak var botonVeloc: UIButton!
    @IBOutlet weak var botonCaudal: UIButton!
    @IBOutlet weak var botonPoten: UIButton!
    @IBOutlet weak var botonPresion: UIButton!
    @IBOutlet weak var botonPropDown: UIButton!
    @IBOutlet var measureSelected: [UIButton]! // in this UICollectionView, I have dragg all the buttons from the Main.storyboard
        
    

    override func viewDidLoad() {
        super.viewDidLoad()

        botonPropDown.addBottomBorderWithColor(color: UIColor.white, width: 3)
        botonLong.addBottomBorderWithColor(color: UIColor.white, width: 1)
        botonLong.addRightBorderWithColor(color: UIColor.white, width: 1)
        botonVolum.addBottomBorderWithColor(color: UIColor.white, width: 1)
        botonVolum.addLeftBorderWithColor(color: UIColor.white, width: 1)
        botonVolum.addRightBorderWithColor(color: UIColor.white, width: 1)
        botonMasa.addBottomBorderWithColor(color: UIColor.white, width: 1)
        botonMasa.addLeftBorderWithColor(color: UIColor.white, width: 1)
        botonMasa.addRightBorderWithColor(color: UIColor.white, width: 1)
        botonTemp.addBottomBorderWithColor(color: UIColor.white, width: 1)
        botonTemp.addLeftBorderWithColor(color: UIColor.white, width: 1)
        botonVeloc.addTopBorderWithColor(color: UIColor.white, width: 1)
        botonVeloc.addRightBorderWithColor(color: UIColor.white, width: 1)
        botonCaudal.addTopBorderWithColor(color: UIColor.white, width: 1)
        botonCaudal.addRightBorderWithColor(color: UIColor.white, width: 1)
        botonCaudal.addLeftBorderWithColor(color: UIColor.white, width: 1)
        botonPoten.addTopBorderWithColor(color: UIColor.white, width: 1)
        botonPoten.addLeftBorderWithColor(color: UIColor.white, width: 1)
        botonPoten.addRightBorderWithColor(color: UIColor.white, width: 1)
        botonPresion.addTopBorderWithColor(color: UIColor.white, width: 1)
        botonPresion.addLeftBorderWithColor(color: UIColor.white, width: 1)
    }

// in this IBAction I have dragged all buttons from Main.storyboard
    @IBAction func propDownPressed(_ sender: UIButton) {
        measureSelected.forEach { (button) in UIView.animate(withDuration: 0.3, animations: {
                button.isHidden = !button.isHidden
                self.view.layoutIfNeeded()
            })
        }
    }
   





Thanks for the help.

Can you describe what you get ?


Which are the buttons in measureSelected ? Are they different from botonLong, botonVolum, …

@IBOutlet var measureSelected: [UIButton]!

You say :

// in this UICollectionView, I have dragg all the buttons from the Main.storyboard

Could you specify which buttons exactly ?


You say:

// in this IBAction I have dragged all buttons from Main.storyboard

Could you specify which buttons exactly ?


Could you add log in the IBAction, to see what's hapenning there :


    @IBAction func propDownPressed(_ sender: UIButton) {
        measureSelected.forEach { (button) in UIView.animate(withDuration: 0.3, animations: {
                button.isHidden = !button.isHidden
                print("Title", button.title)          
                self.view.layoutIfNeeded()
            })
        }
    }

I have 9 buttons like this (I add them on the main screen as followed, constrained to top margin):


Distance Volume Weight Temperature

Speed Flow Power Pressure

Select a measure for conversion


(but I have them in Spanish).


In @IBOutlet var measureSelected: [UIButton]! I have dragged the buttons Distance, Volume, Weight, Temperature, Speed, Flow, Power, Pressure

(botonLong, botonVolum, botonMasa, botonTemp, botonVeloc, botonCaudal, botonPoten, botonPresion).


Then I created an IBAction dragging the first button Distance, and I have dragged to this action the others buttons (Volume, Weight, Temperature, Speed, Flow, Power, Pressure).


When I pressed the button Select a measure for conversion the hidden buttons (Distance, Volume, Weight, Temperature, Speed, Flow, Power, Pressure) appears directly without animated drop down movement.

I see several problems :


- button.isHidden property cannot be animated.

- If you change (just to see) for anotrher propzerty as backgroundColor, you will see an animation


   @IBAction func propDownPressed(_ sender: UIButton) {
        measureSelected.forEach { (button) in
        if self.animatedButton.isHidden {
            button.backgroundColor = UIColor.clear // restore no color, for next time
        }

          button.isHidden = !button.isHidden
          UIView.animate(withDuration: 3, animations: {           // Changed to 3s to see it
                self.animatedButton.backgroundColor = UIColor.red
                self.view.layoutIfNeeded()
            })
        }
    }



You can do the same with borderColor


Instead of animating isHidden, you can animate button.alpha between 0 (hidden) and 1 (not hidden) ; you should have to define a new property for the button, such as isTransparent


   @IBAction func propDownPressed(_ sender: UIButton) {
       isTransparent = !isTransparent     //     Eithet common to all buttons, or you may want a property for each button
        measureSelected.forEach { (button) in
             UIView.animate(withDuration: 3, animations: {
                 if isTransparent {
                     button.backgroundColor = UIColor.red     // Just to better see the effet ; remove after
                     button.alpha = 0.0
                 } else {
                     button.backgroundColor = UIColor.yellow     // Just to better see the effet ; remove after
                     button.alpha = 1.0
                 }
                 self.view.layoutIfNeeded()
             })
        }
    }


Thanks to close the threads you opened, by marking the correct answer once you got the solution.

Hi I still having difficulties to reach what I want.


App example to see what I want to do with topbar buttons : Convertidor de Unidad & Divisa (from oWorld Software).

This is not very clear.

When you click on "select a measure for conversion", how do you want to make the selection ?

- by clicking one of the buttons

Distance Volume Weight Temperature

Speed Flow Power Pressure


- by selecting in a dropDown to appear in "select a measure for conversion"?


If you want, post temporarily your email here, I'll send you mine so that you can mail the complete code.

OK, send by mail at the address I've sent.

You can now delete your email from here.

Buttons block animated as DropDown Menu
 
 
Q