Comments (3)
+1
from expandingmenu.
Perhaps my adoption (very basic) is a starting point:
class SocialButton: NSObject {
private weak var parentController : UIViewController?
private var dimmedView : UIView?
private var mainButton : UIButton?
private var menuButtons = [SocialMenuButton]()
private var menuIsVisible = false
var dimmedViewTopConstraint: NSLayoutConstraint?
var dimmedViewBottomConstraint: NSLayoutConstraint?
var dimmedViewTrailingConstraint: NSLayoutConstraint?
var dimmedViewLeadingConstraint: NSLayoutConstraint?
func configureSocialButtonForViewController(controller: UIViewController) {
self.parentController = controller
self.addDimmedView(controller.view)
self.addButtonView(controller.view)
self.addMenuItems(self.parentController!.view)
self.parentController?.view.layoutIfNeeded()
self.parentController?.view.bringSubviewToFront(mainButton!)
}
func addMenuItems(parentView: UIView){
let socialMenuButton1 = SocialMenuButton(parentView: parentView, referenceView: mainButton!, index: 0, title: "Menu1", image: UIImage(named: "logo")!, highlightedImage: UIImage(named: "logo")!) { () -> Void in
self.showAlert("Menu1")
self.mainButtonTapped()
}
self.menuButtons.append(socialMenuButton1)
let socialMenuButton2 = SocialMenuButton(parentView: parentView, referenceView: mainButton!, index: 1, title: "Menu2", image: UIImage(named: "logo")!, highlightedImage: UIImage(named: "logo")!) { () -> Void in
self.showAlert("Menu2")
self.mainButtonTapped()
}
self.menuButtons.append(socialMenuButton2)
let socialMenuButton3 = SocialMenuButton(parentView: parentView, referenceView: mainButton!, index: 2, title: "Menu3", image: UIImage(named: "logo")!, highlightedImage: UIImage(named: "logo")!) { () -> Void in
self.showAlert("Menu3")
self.mainButtonTapped()
}
self.menuButtons.append(socialMenuButton3)
let socialMenuButton4 = SocialMenuButton(parentView: parentView, referenceView: mainButton!, index: 3, title: "Menu4", image: UIImage(named: "logo")!, highlightedImage: UIImage(named: "logo")!) { () -> Void in
self.showAlert("Menu4")
self.mainButtonTapped()
}
self.menuButtons.append(socialMenuButton4)
}
func hideMenuItems(){
for item in menuButtons{
item.hideItem()
}
}
func showAlert(title: String) {
let alert = UIAlertController(title: title, message: nil, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
self.parentController?.presentViewController(alert, animated: true, completion: nil)
}
func addDimmedView(parentView: UIView){
dimmedView = UIView(frame: CGRect(x: 0,y: 0,width: 10,height: 10))
if let dimmedView = self.dimmedView{
dimmedView.backgroundColor = UIColor.darkGrayColor()
let tap = UITapGestureRecognizer(target: self, action: Selector("mainButtonTapped"))
dimmedView.addGestureRecognizer(tap)
dimmedView.userInteractionEnabled = true
parentView.addSubview(dimmedView)
//Configure autolayout
dimmedView.translatesAutoresizingMaskIntoConstraints = false
let y = UIScreen.mainScreen().bounds.size.height
let x = UIScreen.mainScreen().bounds.size.width
dimmedViewTopConstraint = NSLayoutConstraint(item: dimmedView, attribute:
.Top , relatedBy: .Equal, toItem: parentView, attribute: .Top, multiplier: 1.0, constant: y)
parentView.addConstraint(dimmedViewTopConstraint!)
dimmedViewBottomConstraint = NSLayoutConstraint(item: dimmedView, attribute:
.Bottom , relatedBy: .Equal, toItem: parentView, attribute: .Bottom, multiplier: 1.0, constant: 0)
parentView.addConstraint(dimmedViewBottomConstraint!)
dimmedViewTrailingConstraint = NSLayoutConstraint(item: dimmedView, attribute:
.Trailing , relatedBy: .Equal, toItem: parentView, attribute: .Trailing, multiplier: 1.0, constant: 0)
parentView.addConstraint(dimmedViewTrailingConstraint!)
dimmedViewLeadingConstraint = NSLayoutConstraint(item: dimmedView, attribute:
.Leading , relatedBy: .Equal, toItem: parentView, attribute: .Leading, multiplier: 1.0, constant: x)
parentView.addConstraint(dimmedViewLeadingConstraint!)
}
}
func addButtonView(parentView: UIView){
mainButton = UIButton(frame: CGRect(x:0, y:0, width: 60, height: 60))
if let mainButton = self.mainButton{
mainButton.setImage(UIImage(named: "chooser-button-tab"), forState: UIControlState.Normal)
mainButton.setImage(UIImage(named: "chooser-button-tab-highlighted"), forState: UIControlState.Highlighted)
mainButton.addTarget(self, action: "mainButtonTapped", forControlEvents: UIControlEvents.TouchUpInside)
//mainButton.backgroundColor = UIColor.blueColor()
parentView.addSubview(mainButton)
//Configure autolayout
mainButton.translatesAutoresizingMaskIntoConstraints = false
let bottomConstraint = NSLayoutConstraint(item: mainButton, attribute:
.Bottom, relatedBy: .Equal, toItem: parentView, attribute: .Bottom, multiplier: 1.0, constant: -50)
parentView.addConstraint(bottomConstraint)
let trailingConstraint = NSLayoutConstraint(item: mainButton, attribute:
.Trailing, relatedBy: .Equal, toItem: parentView, attribute: .Trailing, multiplier: 1.0, constant: 0)
parentView.addConstraint(trailingConstraint)
let widthConstraint = NSLayoutConstraint(item: mainButton, attribute:
.Width , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60)
parentView.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: mainButton, attribute:
.Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60)
parentView.addConstraint(heightConstraint)
}
}
func mainButtonTapped(){
if self.menuIsVisible{
//hide
self.hideMenuItems()
self.menuIsVisible = !self.menuIsVisible
self.dimmedView?.alpha = 0.0
self.dimmedViewTopConstraint?.constant = (self.mainButton?.frame.origin.y)!
self.dimmedViewLeadingConstraint?.constant = (self.mainButton?.frame.origin.x)!
self.parentController?.view.layoutIfNeeded()
}
else{
//show
self.dimmedView?.alpha = 0.7
self.dimmedViewTopConstraint?.constant = 0
self.dimmedViewLeadingConstraint?.constant = 0
self.parentController?.view.layoutIfNeeded()
UIView.animateWithDuration(0.3, animations: { () -> Void in
for item in self.menuButtons{
item.showItem()
self.parentController?.view.layoutIfNeeded()
}
}, completion: { (finished) -> Void in
self.menuIsVisible = !self.menuIsVisible
})
}
}
// MARK: - Menu Item Tapped Action
func menuItemTapped(item: SocialMenuButton) {
mainButtonTapped()
}
}
class SocialMenuButton: UIView {
private var menuButton : UIButton?
private var textLabel : UILabel?
weak private var delegate: SocialButton?
private var tappedAction: (() -> Void)?
private var bottomConstraint: NSLayoutConstraint?
private var index = 0
init(parentView: UIView, referenceView: UIView, index: Int, title: String, image: UIImage, highlightedImage: UIImage, itemTapped: (() -> Void)?) {
let itemFrame = CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height)
super.init(frame: itemFrame)
self.index = index
addButton(parentView, referenceView: referenceView, index: index, image: image, highlightedImage: highlightedImage, itemTapped: itemTapped)
addTextLabel(parentView, title: title)
self.tappedAction = itemTapped
self.hideItem()
}
private func addButton(parentView: UIView, referenceView: UIView, index: Int, image: UIImage, highlightedImage: UIImage, itemTapped: (() -> Void)?){
menuButton = UIButton(frame: CGRect(x:0, y:0, width: 30, height: 30))
if let menuButton = menuButton {
menuButton.setImage(image , forState: UIControlState.Normal)
menuButton.setImage(highlightedImage , forState: UIControlState.Highlighted)
menuButton.addTarget(self, action: "tapped", forControlEvents: UIControlEvents.TouchUpInside)
//menuButton.backgroundColor = UIColor.greenColor()
parentView.addSubview(menuButton)
menuButton.translatesAutoresizingMaskIntoConstraints = false
let widthConstraint = NSLayoutConstraint(item: menuButton, attribute:
.Width , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 45)
parentView.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: menuButton, attribute:
.Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 45)
parentView.addConstraint(heightConstraint)
bottomConstraint = NSLayoutConstraint(item: menuButton, attribute:
.Bottom, relatedBy: .Equal, toItem: referenceView, attribute: .Top, multiplier: 1.0, constant:menuButton.bounds.height )
parentView.addConstraint(bottomConstraint!)
let trailingConstraint = NSLayoutConstraint(item: menuButton, attribute:
.Trailing, relatedBy: .Equal, toItem: referenceView, attribute: .Trailing, multiplier: 1.0, constant: -10)
parentView.addConstraint(trailingConstraint)
}
}
private func addTextLabel(parentView: UIView, title: String){
textLabel = UILabel(frame: CGRect(x:0, y:0, width: 100, height: 30))
if let textLabel = textLabel{
textLabel.text = title
//textLabel.backgroundColor = UIColor.greenColor()
textLabel.font = UIFont.systemFontOfSize(22)
textLabel.textColor = UIColor.whiteColor()
let tap = UITapGestureRecognizer(target: self, action: Selector("tapped"))
textLabel.addGestureRecognizer(tap)
textLabel.userInteractionEnabled = true
parentView.addSubview(textLabel)
textLabel.translatesAutoresizingMaskIntoConstraints = false
let trailingConstraint = NSLayoutConstraint(item: textLabel, attribute:
.Trailing, relatedBy: .Equal, toItem: menuButton, attribute: .Leading, multiplier: 1.0, constant: -20)
parentView.addConstraint(trailingConstraint)
let centerYConstraint = NSLayoutConstraint(item: textLabel, attribute:
.CenterY, relatedBy: .Equal, toItem: menuButton, attribute: .CenterY, multiplier: 1.0, constant: 0)
parentView.addConstraint(centerYConstraint)
}
}
func showItem(){
bottomConstraint?.constant = CGFloat(self.index) * -60
self.alpha = 1.0
self.menuButton?.alpha = 1.0
self.textLabel?.alpha = 1.0
}
func hideItem(){
self.alpha = 0.0
self.menuButton?.alpha = 0.0
self.textLabel?.alpha = 0.0
bottomConstraint?.constant = (menuButton?.bounds.height)!
}
func removeSubviews(){
self.textLabel?.removeFromSuperview()
self.menuButton?.removeFromSuperview()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: - Tapped Action
func tapped() {
self.delegate?.menuItemTapped(self)
self.tappedAction?()
}
}
from expandingmenu.
Totally agree, without the auto-layout support the #6 becomes a real issue and makes this nice library not usable in production without hacks.
from expandingmenu.
Related Issues (20)
- Can't run carthage update build HOT 2
- Size is broken add Spacing parameter
- Possible to change the font of titles?
- Customizable menu button' rotationAngle
- Typo In README.md Example Code HOT 1
- Xcode 8.3: 'M_PI' is deprecated WARNING HOT 4
- Typo Error in "item1" Highlighted Image is Pointed to Place in Sample Project
- Closing The Menu
- It's possible to scroll when you have more buttons that the screen support?
- 有oc版本吗?
- Issues with xcode 10/Swift 4.2 HOT 2
- Expanding Position Menu Button
- Animation option not found HOT 8
- App crashes after manual setup for 4.2 support HOT 2
- suggest support for Blur Effect background feature when pop HOT 1
- Ipad-full screen is not being covered HOT 3
- Margin between menu button and first item
- SPM support
- Change menu item image on tap HOT 1
- double line
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expandingmenu.