0

I have two UIButton's created off screen that are animated onto the screen when a button is pressed. However, neither of the button's target functions are never called, as I believe the buttons are not being "pressed". The buttons do work if I simply create them on the screen, so I believe the issue is related to the buttons being animated onto the screen from outside the screens bounds.

Edit: The buttons do animate onto the screen properly and are visible, however pressing them does nothing.

Code:

var isMenuOpen : Bool = false
var button_height : CGFloat = UIScreen.mainScreen().bounds.height/8

var button_view : UIView!
var mylists : UIButton!
var settings : UIButton!

@IBAction func openMenu(sender: AnyObject) {
    UIView.animateWithDuration(0.6, delay: 0.0, options: .CurveEaseOut, animations: {
        self.view.transform = CGAffineTransformMakeTranslation(0.0, self.button_height * (self.isMenuOpen ? 0 : -1))
    }, completion: {finished in
        self.isMenuOpen = !self.isMenuOpen
    })

}

override func viewDidLoad() {
    super.viewDidLoad()
    initButtons()  
}

func initButtons() {
    button_view = UIView(frame: CGRect(x:0.0, y:UIScreen.mainScreen().bounds.height, width:UIScreen.mainScreen().bounds.width, height:button_height))
    button_view.userInteractionEnabled = true

    mylists = UIButton(frame: CGRect(x:0.0, y:0.0, width:UIScreen.mainScreen().bounds.width/2.0, height:button_height))
    mylists.setTitle("My Lists", forState: .Normal)
    mylists.backgroundColor = UIColor(red: 255/255, green: 105/255, blue: 180/255, alpha: 1.0)
    mylists.addTarget(self, action: #selector(showLists(_:)), forControlEvents: .TouchUpInside)
    button_view.addSubview(mylists)

    settings = UIButton(frame: CGRect(x:UIScreen.mainScreen().bounds.width/2.0, y:0.0, width:UIScreen.mainScreen().bounds.width/2.0, height:button_height))
    settings.setTitle("Settings", forState: .Normal)
    settings.backgroundColor = UIColor(red: 135/255, green: 205/255, blue: 236/255, alpha: 1.0)
    settings.addTarget(self, action: #selector(showSettings(_:)), forControlEvents: .TouchUpInside)
    button_view.addSubview(settings)

    self.view.addSubview(button_view)
}

func showLists(sender: UIButton!) {
    print("showLists")
}

func showSettings(sender: UIButton!) {
    print("showSettings")
}

Any help is appreciated. Thanks!

Dando18
  • 622
  • 9
  • 22

1 Answers1

1

I don't the exact goal of your transition, but it looks like the view that is holding the buttons button_view is never being transition onto the screen.

Are you trying to make each button go to the screen or are you trying to make both buttons go to the screen inside a container (button_view_)? If you are trying to do both, I would recommend doing

UIView.animateWithDuration(0.6, delay: 0.0, options: .CurveEaseOut, animations: {
    self.button_view.transform = CGAffineTransformMakeTranslation(0.0, -1*(UIScreen.mainScreen().bounds.height))
}, completion: {finished in
    self.isMenuOpen = !self.isMenuOpen
})

This is based off the assumption you are trying to move the box to the screen as a whole.

Edit:

Ahh! I see what is happening! So you are moving self.view up, which you never wanna move a UIViewController.view. Move a container of the view. So instead of moving self.view, move button_view. Moving a UIController.view removes all of the underneath layers and functionality of a UIViewController

impression7vx
  • 1,728
  • 1
  • 20
  • 50
  • The buttons are visible after the animation though. Wouldn't that mean that their container would also be on the screen? – Dando18 Jul 10 '16 at 22:44
  • So visible but not clickable? – impression7vx Jul 10 '16 at 22:47
  • Correct and the buttons never highlight when pressed so I assume they're disabled or their frame is off screen somehow, but debugging and `print` statements have gotten me nowhere. – Dando18 Jul 10 '16 at 22:50
  • Well what you are doing is moving the self.view out of the way, and then you are moving menu_button. Both of which have zero regards toward either of the other 2 buttons, or the view containing them. So I am not sure how you can see them. – impression7vx Jul 10 '16 at 22:50
  • `button_view`, which contains both buttons, is a subview of `self.view` – Dando18 Jul 10 '16 at 22:54
  • Ahh! I see what is happening! So you are moving self.view up, which you never wanna move a UIViewController.view. Move a container of the view. So instead of moving self.view, move button_view. Moving a UIController.view removes all of the underneath layers and functionality of a UIViewController. – impression7vx Jul 10 '16 at 22:56
  • Thanks. That somewhat fixed the issue. If you update your answer I'll mark it as correct. – Dando18 Jul 10 '16 at 23:08
  • Well would you like help with anything else? Just update your question and I can look at whatever else is causing issues. – impression7vx Jul 11 '16 at 00:00
  • My other issue was that there were other components in the view that I wished to be animated up with the view. I solved this, however, by animating each of the view's subviews upwards like I do with the buttons and leaving the view alone. Thanks tho. – Dando18 Jul 11 '16 at 00:16
  • Well if you added them to a container, such as button_view, then just animate the container, and they would all go with it! I have been taught that containers are very useful, especially in situations of animations and grouping. – impression7vx Jul 11 '16 at 00:17