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
}
}
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