Release 0.1.0

This commit is contained in:
giuseppenuc committed 2015-08-03 12:52:31 +02:00
1 parent fd2c9b2650
commit bb19fe0f7d
66 files changed
+3772 -14

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
//
// LBHamburgerButton.h
// Version 1.0
//
// Created by Bang Nguyen on 26/07/2014.
//
// This code is distributed under the terms and conditions of the MIT license.
// 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.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
typedef enum {
/** Show back (<-) button after switch state/animate. */
LBHamburgerButtonTypeBackButton = 0,
/** Show close (X) button after switch state/animate. */
LBHamburgerButtonTypeCloseButton
// other type
} LBHamburgerButtonType;
typedef enum {
/** Initialize state, with 3 lines (hamburger). */
LBHamburgerButtonStateHamburger = 0,
/** State happened after animate. */
LBHamburgerButtonStateNotHamburger
} LBHamburgerButtonState;
/**
* LBHamburgerButton is a sub-class of UIButton, displays a hamburger button
* and other state button without using image
*
* This is a simple button with 2 state. At initialization, it has state "Hamburger"
* which means 3 lines. After switch state, it has state "Not hamburger" and
* displays a back button/a close button.
*
* This button support 2 type:
* - LBHamburgerButtonTypeBackButton - display <- after switch state
* - LBHamburgerButtonTypeCloseButton - display X after switch state
*
* Those type can be setted when initialization or manually at any time/any event.
*
*/
@interface LBHamburgerButton : UIButton
/**
* Current state of hamburger button.
*/
@property (readonly) LBHamburgerButtonState hamburgerState;
/**
* Type of hamburger button.
*/
@property LBHamburgerButtonType hamburgerType;
/**
* Time for animation. Default is 0.5f.
*/
@property CFTimeInterval hamburgerAnimationDuration;
/**
* Full contructor that initializes the button with default state.
*
* @param frame Frame of the button in superview.
* @param type Type of button, Back or Close or etc.
* @param lineWidth Width of each line in hamburger.
* @param lineHeight Height of each line in hamburger.
* @param lineSpacing Distance between lines.
* @param lineCenter Position of 2nd line in hamburger (which means center of group of 3)
* @param color Color of lines.
*
*/
- (instancetype)initWithFrame:(CGRect)frame
withHamburgerType:(LBHamburgerButtonType)type
lineWidth:(CGFloat)lineWidth
lineHeight:(CGFloat)lineHeight
lineSpacing:(CGFloat)lineSpacing
lineCenter:(CGPoint)lineCenter
color:(UIColor*)color;
/**
* Set up default state of button which has 3 lines.
*
* @param type Type of button, Back or Close or etc.
* @param lineWidth Width of each line in hamburger.
* @param lineHeight Height of each line in hamburger.
* @param lineSpacing Distance between lines.
* @param lineCenter Position of 2nd line in hamburger (which means center of group of 3)
* @param color Color of lines.
*
*/
- (void)setUpHamburgerWithType:(LBHamburgerButtonType)type
lineWidth:(CGFloat)lineWidth
lineHeight:(CGFloat)lineHeight
lineSpacing:(CGFloat)lineSpacing
lineCenter:(CGPoint)lineCenter
color:(UIColor*)color;
/**
* Animate from current state to other state.
*/
- (void)switchState;
/**
* Animate from current state to specfied state.
*/
- (void)setState:(LBHamburgerButtonState)state;
@end
/**
* Supporting category of CALayer
*/
@interface CALayer (LBAddAnimationAndValue)
/**
* Add both animation and its end value to CALayer
*/
- (void)addAnimation:(CAAnimation *)anim andValue:(NSValue*)value forKeyPath:(NSString *)keyPath;
@end
/**
* Supporting category of UIBezierPath
* Helps creating a curve path for animation
*/
@interface UIBezierPath (LBAnimateBezierPath)
/**
* Create a curve path with ease
*/
+ (UIBezierPath*)animateBezierPathFrom:(CGPoint)startPoint toPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
@end
+274
View File
@@ -0,0 +1,274 @@
//
// LBHamburgerButton.m
// HamburgerButton
//
// Created by Bang Nguyen on 26/07/2014.
// Copyright (c) Năm 2014 Bang Nguyen. All rights reserved.
//
#import "LBHamburgerButton.h"
@interface LBHamburgerButton () {
CGFloat _lineHeight, _lineWidth, _lineSpacing;
CGPoint _lineCenter;
NSArray* _lineArray;
BOOL _lineCreated;
}
@end
@implementation LBHamburgerButton
#pragma mark -
#pragma mark - Public functions
- (instancetype)initWithFrame:(CGRect)frame withHamburgerType:(LBHamburgerButtonType)type lineWidth:(CGFloat)lineWidth lineHeight:(CGFloat)lineHeight lineSpacing:(CGFloat)lineSpacing lineCenter:(CGPoint)lineCenter color:(UIColor *)color {
if (self = [super initWithFrame:frame]) {
_hamburgerAnimationDuration = 0.5;
_hamburgerState = LBHamburgerButtonStateHamburger;
[self setUpHamburgerWithType:type
lineWidth:lineWidth
lineHeight:lineHeight
lineSpacing:lineSpacing
lineCenter:lineCenter
color:color];
}
return self;
}
- (void)setUpHamburgerWithType:(LBHamburgerButtonType)type lineWidth:(CGFloat)lineWidth lineHeight:(CGFloat)lineHeight lineSpacing:(CGFloat)lineSpacing lineCenter:(CGPoint)lineCenter color:(UIColor *)color {
if (_lineCreated) {
// Lines have been created, do nothing.
return;
}
_hamburgerType = type;
_lineWidth = lineWidth;
_lineHeight = lineHeight;
_lineSpacing = lineSpacing;
_lineCenter = lineCenter;
CAShapeLayer* topLine = [self createLayerWithFrame:CGRectMake(0, 0, lineWidth, lineHeight) andColor:color atPosition:CGPointMake(lineCenter.x, lineCenter.y - lineHeight - lineSpacing)];
CAShapeLayer* middleLine = [self createLayerWithFrame:CGRectMake(0, 0, lineWidth, lineHeight) andColor:color atPosition:lineCenter];
CAShapeLayer* bottomLine = [self createLayerWithFrame:CGRectMake(0, 0, lineWidth, lineHeight) andColor:color atPosition:CGPointMake(lineCenter.x, lineCenter.y + lineHeight + lineSpacing)];
_lineArray = @[ topLine, middleLine, bottomLine ];
for (CAShapeLayer* layer in _lineArray) {
[self.layer addSublayer:layer];
}
_lineCreated = YES;
}
- (void)switchState {
[self animateButtonForward:(_hamburgerState == LBHamburgerButtonStateHamburger)];
_hamburgerState = (_hamburgerState == LBHamburgerButtonStateHamburger) ? LBHamburgerButtonStateNotHamburger : LBHamburgerButtonStateHamburger;
}
-(void)setState:(LBHamburgerButtonState)state
{
if(_hamburgerState == state)
return;
else
[self switchState];
}
#pragma mark -
#pragma mark - Private functions
- (void)animateButtonForward:(BOOL)forward {
NSArray* anims = @[ [self keyframeAnimationsOfLineIndex:0 forward:forward],
[self keyframeAnimationsOfLineIndex:1 forward:forward],
[self keyframeAnimationsOfLineIndex:2 forward:forward] ];
[CATransaction begin];
[CATransaction setAnimationDuration:_hamburgerAnimationDuration];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0]];
for (NSArray* animations in anims) {
NSInteger index = [anims indexOfObject:animations];
for (CAKeyframeAnimation* anim in animations) {
CAShapeLayer* layer = _lineArray[index];
if (!anim.removedOnCompletion) {
[layer addAnimation:anim forKey:anim.keyPath];
} else {
[layer addAnimation:anim andValue:anim.values.lastObject forKeyPath:anim.keyPath];
}
}
}
[CATransaction commit];
}
- (NSArray*)keyframeAnimationsOfLineIndex:(NSInteger)index forward:(BOOL)forward {
switch (_hamburgerType) {
case LBHamburgerButtonTypeBackButton:
{
switch (index) {
case 0:
{
CAKeyframeAnimation* animRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animRotate.values = forward ? @[ @0, @(M_PI*5/4) ] : @[ @(M_PI*5/4), @0];
animRotate.calculationMode = kCAAnimationCubic;
animRotate.keyTimes = @[ @0, @0.33, @0.73, @1.0];
CGPoint startPoint = forward ? CGPointMake(_lineCenter.x, _lineCenter.y - _lineHeight - _lineSpacing) : CGPointMake(_lineCenter.x - [self scale:10], _lineCenter.y + _lineHeight + [self scale:7.2]);
CGPoint endPoint = forward ? CGPointMake(_lineCenter.x - [self scale:10], _lineCenter.y + _lineHeight + [self scale:7.2]) : CGPointMake(_lineCenter.x, _lineCenter.y - _lineHeight - _lineSpacing);
CGPoint controlPoint = CGPointMake(_lineCenter.x + [self scale:15], _lineCenter.y);
CAKeyframeAnimation* animPosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animPosition.path = [UIBezierPath animateBezierPathFrom:startPoint toPoint:endPoint controlPoint:controlPoint].CGPath;
animPosition.removedOnCompletion = NO;
animPosition.fillMode = kCAFillModeForwards;
return @[ animRotate, animPosition ];
}
case 1:
{
CAKeyframeAnimation* animRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animRotate.values = forward ? @[ @0, @(M_PI) ] : @[ @(M_PI), @0];
return @[ animRotate ];
}
case 2:
{
CAKeyframeAnimation* animRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animRotate.values = forward ? @[ @0, @(-M_PI*5/4) ] : @[ @(-M_PI*5/4), @0];
animRotate.calculationMode = kCAAnimationCubic;
animRotate.keyTimes = @[ @0, @0.33, @0.73, @1.0];
CGPoint startPoint = forward ? CGPointMake(_lineCenter.x, _lineCenter.y + _lineHeight + _lineSpacing) : CGPointMake(_lineCenter.x - [self scale:10], _lineCenter.y - _lineHeight - [self scale:7.2]);
CGPoint endPoint = forward ? CGPointMake(_lineCenter.x - [self scale:10], _lineCenter.y - _lineHeight - [self scale:7.2]) : CGPointMake(_lineCenter.x, _lineCenter.y + _lineHeight + _lineSpacing);
CGPoint controlPoint = CGPointMake(_lineCenter.x + [self scale:15], _lineCenter.y);
CAKeyframeAnimation* animPosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animPosition.path = [UIBezierPath animateBezierPathFrom:startPoint toPoint:endPoint controlPoint:controlPoint].CGPath;
animPosition.removedOnCompletion = NO;
animPosition.fillMode = kCAFillModeForwards;
return @[ animRotate, animPosition ];
}
default:
break;
}
break;
}
case LBHamburgerButtonTypeCloseButton:
{
switch (index) {
case 0:
{
CAKeyframeAnimation* animRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animRotate.values = forward ? @[ @0, @(-M_PI*5/4) ] : @[ @(-M_PI*5/4), @0];
animRotate.calculationMode = kCAAnimationCubic;
animRotate.keyTimes = @[ @0, @0.33, @0.73, @1.0];
CGPoint startPoint = forward ? CGPointMake(_lineCenter.x, _lineCenter.y - _lineHeight - _lineSpacing) : _lineCenter;
CGPoint endPoint = forward ? _lineCenter : CGPointMake(_lineCenter.x, _lineCenter.y - _lineHeight - _lineSpacing);
CGPoint controlPoint = CGPointMake(_lineCenter.x + [self scale:20], _lineCenter.y - _lineHeight - [self scale:5]);
CAKeyframeAnimation* animPosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animPosition.path = [UIBezierPath animateBezierPathFrom:startPoint toPoint:endPoint controlPoint:controlPoint].CGPath;
animPosition.removedOnCompletion = NO;
animPosition.fillMode = kCAFillModeForwards;
return @[ animRotate, animPosition ];
}
case 1:
{
CAKeyframeAnimation* animRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animRotate.values = forward ? @[ @0, @(M_PI) ] : @[ @(M_PI), @0];
CAKeyframeAnimation* animScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
animScale.values = forward ? @[ @1, @0.1 ] : @[ @0.1, @1 ];
return @[ animRotate, animScale ];
}
case 2:
{
CAKeyframeAnimation* animRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animRotate.values = forward ? @[ @0, @(M_PI*5/4) ] : @[ @(M_PI*5/4), @0];
animRotate.calculationMode = kCAAnimationCubic;
animRotate.keyTimes = @[ @0, @0.33, @0.73, @1.0];
CGPoint startPoint = forward ? CGPointMake(_lineCenter.x, _lineCenter.y + _lineHeight + _lineSpacing) : _lineCenter;
CGPoint endPoint = forward ? _lineCenter : CGPointMake(_lineCenter.x, _lineCenter.y + _lineHeight + _lineSpacing);
CGPoint controlPoint = CGPointMake(_lineCenter.x - [self scale:20], _lineCenter.y + _lineHeight + [self scale:5]);
CAKeyframeAnimation* animPosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animPosition.path = [UIBezierPath animateBezierPathFrom:startPoint toPoint:endPoint controlPoint:controlPoint].CGPath;
animPosition.removedOnCompletion = NO;
animPosition.fillMode = kCAFillModeForwards;
return @[ animRotate, animPosition ];
}
default:
break;
}
break;
}
default:
break;
}
return nil;
}
- (CAShapeLayer*)createLayerWithFrame:(CGRect)frame andColor:(UIColor*)color atPosition:(CGPoint)point {
CAShapeLayer* layer = [CAShapeLayer new];
UIBezierPath* path = [UIBezierPath new];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(frame.size.width, 0)];
layer.path = path.CGPath;
layer.lineWidth = frame.size.height;
layer.strokeColor = color.CGColor;
CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
layer.bounds = CGPathGetBoundingBox(bound);
layer.position = point;
return layer;
}
- (CGFloat)scale:(CGFloat)value {
return value/(50/_lineWidth);
}
@end
#pragma mark -
#pragma mark - Supporting Category
#pragma mark - CALayer
@implementation CALayer (LBAddAnimationAndValue)
- (void)addAnimation:(CAAnimation *)anim andValue:(NSValue *)value forKeyPath:(NSString *)keyPath {
[self addAnimation:anim forKey:keyPath];
[self setValue:value forKeyPath:keyPath];
}
@end
#pragma mark - UIBezierPath
@implementation UIBezierPath (LBAnimateBezierPath)
+ (UIBezierPath*)animateBezierPathFrom:(CGPoint)startPoint toPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint {
UIBezierPath* path = [UIBezierPath new];
[path moveToPoint:startPoint];
[path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
return path;
}
@end
View File
Whitespace-only changes.