Bang Nguyen
@luongbang152
http://luongbang.me/2014/07/great-animation-hamburger-button/
LBHamburgerButton is an iOS control that displays 3 lines (hamburger) and other icon (back, close...) without using image. It has a beautiful, clean and smooth transition between 2 state like Google Material Design or "close button" of Paper app.
LBHamburgerButton use QuartzCore framework, so dont forget to add this framework. ARC (iOS 5+) is also required.
First, you need to copy 2 files LBHamburgerButton.h/.m to your project. Then, import them to your class
#import "LBHamburgerButton.h"
This is a sub-class of UIButton. So you can init a simple button and call set-up method:
buttonHamburger = [[LBHamburgerButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[buttonHamburger setUpHamburgerWithType:LBHamburgerButtonTypeCloseButton
lineWidth:50
lineHeight:50/6
lineSpacing:5
lineCenter:CGPointMake(50, 50)
color:[UIColor whiteColor]];
var hamburgerButton = LBHamburgerButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
hamburgerButton.setUpHamburger(type: LBHamburgerButtonType.closeButton, lineWidth: 50, lineHeight: 50/6, lineSpacing: 5, lineCenter: CGPoint(x: 50, y: 50), color: UIColor.white)
or use full contructor like this:
buttonHamburger = [[LBHamburgerButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
withHamburgerType:LBHamburgerButtonTypeCloseButton
lineWidth:50
lineHeight:50/6
lineSpacing:5
lineCenter:CGPointMake(50, 50)
color:[UIColor whiteColor]];
var hamburgerButton = LBHamburgerButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100), type: LBHamburgerButtonType.closeButton, lineWidth: 50, lineHeight: 50/6, lineSpacing: 5, lineCenter: CGPoint(x: 50, y: 50), color: UIColor.white)
Set other propertises, add target add it to view like UIButton.
[buttonHamburger setCenter:CGPointMake(120, 120)];
[buttonHamburger setBackgroundColor:[UIColor blackColor]];
[buttonHamburger addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonHamburger];
hamburgerButton.center = CGPoint(x: 120, y: 120)
hamburgerButton.backgroundColor = UIColor.black
hamburgerButton.addTarget(self, action: #selector(LBViewController.buttonPressed(_:)), forControlEvents: UIControlEvents.touchUpInside)
self.view.addSubview(hamburgerButton)
When you want to switch state, just call switchState
.
- (void)buttonPressed:(id)sender {
LBHamburgerButton* btn = (LBHamburgerButton*)sender;
[btn switchState];
}
@objc func buttonPressed(sender: UIButton) {
var btn = sender as LBHamburgerButton
btn.switchState()
}
Version 1.0.2 Convert to Swift 3
Version 1.0.1 Convert to Swift
Version 1.0
Initial release Two type
- Back (<-)
- Close (x)
Copyright (c) 2014 Bang Nguyen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.