-1

So I have a button that when pressed, will animate/unhide 3 other buttons and vice versa when pressed again. The animation works perfectly fine everytime BUT the first time being pressed. Not sure why it's not working the first time as it stopped working suddenly...

EDIT: i narrowed it down to the viewDidLoad function, it seems that it is not setting the buttons the to main button's location on load. not sure how to fix it.

here is the code:

class MainMenuViewController: UIViewController {

    @IBOutlet weak var more: UIButton!
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var addItemButton: UIButton!
    @IBOutlet weak var settingsButton: UIButton!

    var addButtonPoints: CGPoint!
    var addItemButtonPoints: CGPoint!
    var settingsButtonPoints: CGPoint!
    var moreButtonPressed: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        addButtonPoints = addButton.center
        addItemButtonPoints = addItemButton.center
        settingsButtonPoints = settingsButton.center

        addButton.center = more.center // moving button towards the center of the main button
        addItemButton.center = more.center
        settingsButton.center = more.center
    }

    @IBAction func mainMenuTapped(_ sender: Any) {
        if moreButtonPressed == true { // collapse
            UIView.animate(withDuration: 0.3, animations: {
                self.addItemButton.alpha = 0 // hiding the buttons
                self.settingsButton.alpha = 0
                self.addButton.alpha = 0

                self.addItemButton.center = self.more.center // moving buttons
                self.settingsButton.center = self.more.center
                self.addButton.center = self.more.center
            })
            moreButtonPressed = false
        }
        else { // expand
            UIView.animate(withDuration: 0.3, animations: {
                self.addItemButton.alpha = 1
                self.settingsButton.alpha = 1
                self.addButton.alpha = 1

                self.addItemButton.center = self.addItemButtonPoints
                self.settingsButton.center = self.settingsButtonPoints
                self.addButton.center = self.addButtonPoints
            })
            moreButtonPressed = true
        }
    }

what the buttons look like

the left is what the app looks like upon launch, and the right is what it looks like inside storyboard (usually left 3 buttons are hidden but i unhid it to show it better) as seen in the code, the viewDidLoad sets the buttons to the location of the main menu

  • 2
    Check what you're trying to do. Your comment 'unhiding the buttons' is next to setting `.alpha = 0`, which would make the buttons invisible. Your first time through the code (`moreButtonPressed == false`) you move the buttons to their original spot (`addButton.center = self.addButtonPoints`, but the related comment is in the other clause. Check if you have your logic correct. – Chris Shaw Dec 20 '19 at 05:31
  • that was my mistake, i added those comments right before posting, fixed now. – mozerellacheese Dec 20 '19 at 05:34
  • Can you tell us (add to the question) how the initial state appears? On entering `viewDidLoad` the buttons are in their visible place, and you move them out of the way with the `= more.center` lines? And is that working ok? Can you check via the debugger which clause executes the first time and that the correct 'make visible' path is taken? – Chris Shaw Dec 20 '19 at 05:42
  • i inserted an image in the original post. on entering viewDidLoad, the buttons are hidden (alpha = 0) and moved to the main button with more.center yes, it is working fine EXCEPT for the first time pressing it. not exactly sure how to use the debugger, but using a print statement, it seems to go through the else statement (the correct one) – mozerellacheese Dec 20 '19 at 05:52
  • also, i believe i narrowed the problem down to the viewDidLoad. I think the more.center lines are not working but Im not sure why as it works fine for the animation lines in the if statements – mozerellacheese Dec 20 '19 at 05:54

1 Answers1

0

Because your logic is inaccurate.

When the user first tapped your button, function triggered and enters the else case. Because moreButtonPressed is false at the beginning.

It sets buttons' alphas to 1 and you cannot see any change. Because of all of the alphas already 1.


Instead of that solution, you can change moreButtonPressed name the more meaningful alternative like isHidden and use it as a state like this:

var isHidden: Bool = false // Button not hidden at the beginning
@IBAction func mainMenuTapped(_ sender: Any) {
    if isHidden {
        UIView.animate(withDuration: 0.3, animations: {
            self.addButton.alpha = 1
        })
        isHidden = false // We set alpha to 1 and it is visible now.
    }
    else {
        UIView.animate(withDuration: 0.3, animations: {
            self.addButton.alpha = 0
        })
        isHidden = true // We set alpha to 0
    }
}
Mahmut Acar
  • 713
  • 2
  • 7
  • 26
  • the buttons are already set to alpha = 0 inside the storyboard. so in my head it does make sense to enter the else statement. I did try your code (changed it so that it corresponds to when buttons start at alpha=0) and it still does not work. – mozerellacheese Dec 20 '19 at 06:04
  • In that case, change `var isHidden: Bool = true` at definition in my code. – Mahmut Acar Dec 20 '19 at 06:07