Created
January 8, 2019 18:12
-
-
Save damodarnamala/d9542b79870d386a44ec8b9b6fdb36e9 to your computer and use it in GitHub Desktop.
IOS Buttons Menu with image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MenuViewController: UIViewController { | |
var buttons: [UIButton] = [] | |
var imageView : UIImageView! | |
var constraintLeft : NSLayoutConstraint! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
addMenuItems() | |
} | |
func addMenuItems() { | |
for _ in 0...4 { | |
let button : UIButton = UIButton() | |
self.view.addSubview(button) | |
button.backgroundColor = UIColor.darkGray | |
button.addTarget(self, action: #selector(didSelectedButton(_:)), for: .touchUpInside) | |
self.buttons.append(button) | |
} | |
for (index, button) in buttons.enumerated() { | |
button.setTitle(" \(index)", for: .normal) | |
if index == 0 { | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true | |
button.widthAnchor.constraint(greaterThanOrEqualToConstant: 1).isActive = true | |
button.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true | |
self.imageView = UIImageView() | |
self.view.addSubview(self.imageView) | |
self.imageView.backgroundColor = UIColor.red | |
self.imageView.translatesAutoresizingMaskIntoConstraints = false | |
constraintLeft = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0) | |
constraintLeft.isActive = true | |
self.imageView.widthAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1).isActive = true | |
self.imageView.heightAnchor.constraint(equalToConstant: 4).isActive = true | |
self.imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant:250).isActive = true | |
} | |
else { | |
let preButton = buttons[index - 1] | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.leftAnchor.constraint(equalTo: preButton.rightAnchor, constant: 0).isActive = true | |
button.widthAnchor.constraint(equalTo: preButton.widthAnchor, multiplier: 1).isActive = true | |
button.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true | |
if index == self.buttons.count - 1 { | |
button.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true | |
} | |
} | |
} | |
} | |
@objc func didSelectedButton(_ button:UIButton) { | |
UIView.animate(withDuration: 0.5, delay: 0.1, usingSpringWithDamping: 0.85, initialSpringVelocity: 1, options: .curveEaseInOut, animations: { | |
self.constraintLeft.isActive = false | |
self.constraintLeft = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0) | |
self.constraintLeft.isActive = true | |
self.view.layoutIfNeeded() | |
}, completion:nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment