Skip to content

Commit

Permalink
Updated to 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
a1anyip committed Mar 29, 2016
1 parent 29b4fc8 commit 93faa3d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 44 deletions.
4 changes: 2 additions & 2 deletions AYVibrantButton.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "AYVibrantButton"
s.version = "1.0.3"
s.version = "1.0.4"
s.summary = "A stylish button with iOS 8 vibrancy effect"
s.homepage = "https://github.com/a1anyip/AYVibrantButton"
s.license = { :type => "MIT" }
s.author = { "Alan Yip" => "[email protected]" }
s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/a1anyip/AYVibrantButton.git", :tag => "1.0.3" }
s.source = { :git => "https://github.com/a1anyip/AYVibrantButton.git", :tag => "1.0.4" }
s.source_files = "AYVibrantButton"
s.framework = "UIKit"
s.requires_arc = true
Expand Down
30 changes: 22 additions & 8 deletions AYVibrantButton/AYVibrantButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@import UIKit;
#import <UIKit/UIKit.h>

/** AYVibrantButton **/

Expand All @@ -38,11 +38,12 @@ typedef enum {

} AYVibrantButtonStyle;

@interface AYVibrantButton : UIButton
@interface AYVibrantButton : UIControl

@property (nonatomic, assign) BOOL animated;
@property (nonatomic, assign) CGFloat animationDuration;
@property (nonatomic, assign) CGFloat alpha;
@property (nonatomic, assign) CGFloat invertAlphaHighlighted;
@property (nonatomic, assign) CGFloat translucencyAlphaNormal;
@property (nonatomic, assign) CGFloat translucencyAlphaHighlighted;
@property (nonatomic, assign) CGFloat cornerRadius;
Expand All @@ -57,8 +58,11 @@ typedef enum {
@property (nonatomic, strong) UIVibrancyEffect *vibrancyEffect;
#endif

// the background color when vibrancy effect is nil, or not supported.
@property (nonatomic, strong) UIColor *backgroundColor;
// the deprecated background color
@property (nonatomic, strong) UIColor *backgroundColor DEPRECATED_MSG_ATTRIBUTE("Use tintColor instead.");

// the tint color when vibrancy effect is nil, or not supported.
@property (nonatomic, strong) UIColor *tintColor;

// this is the only method to initialize a vibrant button
- (instancetype)initWithFrame:(CGRect)frame style:(AYVibrantButtonStyle)style;
Expand Down Expand Up @@ -88,8 +92,11 @@ typedef enum {
@property (nonatomic, copy) NSString *text;
@property (nonatomic, strong) UIFont *font;

// background color
@property (nonatomic, strong) UIColor *backgroundColor;
// the deprecated background color
@property (nonatomic, strong) UIColor *backgroundColor DEPRECATED_MSG_ATTRIBUTE("Use tintColor instead.");

// tint color
@property (nonatomic, strong) UIColor *tintColor;

- (instancetype)initWithStyle:(AYVibrantButtonOverlayStyle)style;

Expand All @@ -99,8 +106,12 @@ typedef enum {

@interface AYVibrantButtonGroup : UIView

@property (nonatomic, readonly) NSArray *buttons;
@property (nonatomic, readonly) NSUInteger buttonCount;

@property (nonatomic, assign) BOOL animated;
@property (nonatomic, assign) CGFloat animationDuration;
@property (nonatomic, assign) CGFloat invertAlphaHighlighted;
@property (nonatomic, assign) CGFloat translucencyAlphaNormal;
@property (nonatomic, assign) CGFloat translucencyAlphaHighlighted;
@property (nonatomic, assign) CGFloat cornerRadius;
Expand All @@ -112,8 +123,11 @@ typedef enum {
@property (nonatomic, strong) UIVibrancyEffect *vibrancyEffect;
#endif

// the background color when vibrancy effect is nil, or not supported.
@property (nonatomic, strong) UIColor *backgroundColor;
// the deprecated background color
@property (nonatomic, strong) UIColor *backgroundColor DEPRECATED_MSG_ATTRIBUTE("Use tintColor instead.");

// the tint color when vibrancy effect is nil, or not supported.
@property (nonatomic, strong) UIColor *tintColor;

- (instancetype)initWithFrame:(CGRect)frame buttonTitles:(NSArray *)buttonTitles style:(AYVibrantButtonStyle)style;
- (instancetype)initWithFrame:(CGRect)frame buttonIcons:(NSArray *)buttonIcons style:(AYVibrantButtonStyle)style;
Expand Down
111 changes: 77 additions & 34 deletions AYVibrantButton/AYVibrantButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@

#define kAYVibrantButtonDefaultAnimationDuration 0.15
#define kAYVibrantButtonDefaultAlpha 1.0
#define kAYVibrantButtonDefaultInvertAlphaHighlighted 1.0
#define kAYVibrantButtonDefaultTranslucencyAlphaNormal 1.0
#define kAYVibrantButtonDefaultTranslucencyAlphaHighlighted 0.5
#define kAYVibrantButtonDefaultCornerRadius 4.0
#define kAYVibrantButtonDefaultRoundingCorners UIRectCornerAllCorners
#define kAYVibrantButtonDefaultBorderWidth 0.6
#define kAYVibrantButtonDefaultFontSize 14.0
#define kAYVibrantButtonDefaultBackgroundColor [UIColor whiteColor]
#define kAYVibrantButtonDefaultTintColor [UIColor whiteColor]

/** AYVibrantButton **/

@interface AYVibrantButton () {

__strong UIColor *_backgroundColor;
__strong UIColor *_tintColor;
}

@property (nonatomic, assign) AYVibrantButtonStyle style;
Expand All @@ -58,6 +59,7 @@ @interface AYVibrantButton () {
@property (nonatomic, assign) BOOL hideRightBorder;

- (void)createOverlays;
- (void)updateOverlayAlpha;

@end

Expand All @@ -66,7 +68,7 @@ - (void)createOverlays;
@interface AYVibrantButtonOverlay () {

__strong UIFont *_font;
__strong UIColor *_backgroundColor;
__strong UIColor *_tintColor;
}

@property (nonatomic, assign) AYVibrantButtonOverlayStyle style;
Expand Down Expand Up @@ -115,6 +117,7 @@ - (instancetype)initWithFrame:(CGRect)frame style:(AYVibrantButtonStyle)style {
_cornerRadius = kAYVibrantButtonDefaultCornerRadius;
_roundingCorners = kAYVibrantButtonDefaultRoundingCorners;
_borderWidth = kAYVibrantButtonDefaultBorderWidth;
_invertAlphaHighlighted = kAYVibrantButtonDefaultInvertAlphaHighlighted;
_translucencyAlphaNormal = kAYVibrantButtonDefaultTranslucencyAlphaNormal;
_translucencyAlphaHighlighted = kAYVibrantButtonDefaultTranslucencyAlphaHighlighted;
_alpha = kAYVibrantButtonDefaultAlpha;
Expand Down Expand Up @@ -162,15 +165,34 @@ - (void)createOverlays {
[self addSubview:self.normalOverlay];
[self addSubview:self.highlightedOverlay];
#endif

}

- (void)updateOverlayAlpha {

if (self.activeTouch) {
if (self.style == AYVibrantButtonStyleInvert) {
self.normalOverlay.alpha = 0.0;
self.highlightedOverlay.alpha = self.invertAlphaHighlighted * self.alpha;
} else if (self.style == AYVibrantButtonStyleTranslucent || self.style == AYVibrantButtonStyleFill) {
self.normalOverlay.alpha = self.translucencyAlphaHighlighted * self.alpha;
}
} else {
if (self.style == AYVibrantButtonStyleInvert) {
self.normalOverlay.alpha = self.alpha;
self.highlightedOverlay.alpha = 0.0;
} else if (self.style == AYVibrantButtonStyleTranslucent || self.style == AYVibrantButtonStyleFill) {
self.normalOverlay.alpha = self.translucencyAlphaNormal * self.alpha;
}
}
}

#pragma mark - Control Event Handlers

- (void)touchDown {

self.activeTouch = YES;

void(^update)(void) = ^(void) {
if (self.style == AYVibrantButtonStyleInvert) {
self.normalOverlay.alpha = 0.0;
Expand All @@ -190,7 +212,7 @@ - (void)touchDown {
- (void)touchUp {

self.activeTouch = NO;

void(^update)(void) = ^(void) {
if (self.style == AYVibrantButtonStyleInvert) {
self.normalOverlay.alpha = self.alpha;
Expand All @@ -209,31 +231,30 @@ - (void)touchUp {

#pragma mark - Override Getters

- (UIColor *)backgroundColor {
return _backgroundColor == nil ? kAYVibrantButtonDefaultBackgroundColor : _backgroundColor;
- (UIColor *)tintColor {
return _tintColor == nil ? kAYVibrantButtonDefaultTintColor : _tintColor;
}

#pragma mark - Override Setters

- (void)setAlpha:(CGFloat)alpha {

_alpha = alpha;

if (self.activeTouch) {
if (self.style == AYVibrantButtonStyleInvert) {
self.normalOverlay.alpha = 0.0;
self.highlightedOverlay.alpha = self.alpha;
} else if (self.style == AYVibrantButtonStyleTranslucent || self.style == AYVibrantButtonStyleFill) {
self.normalOverlay.alpha = self.translucencyAlphaHighlighted * self.alpha;
}
} else {
if (self.style == AYVibrantButtonStyleInvert) {
self.normalOverlay.alpha = self.alpha;
self.highlightedOverlay.alpha = 0.0;
} else if (self.style == AYVibrantButtonStyleTranslucent || self.style == AYVibrantButtonStyleFill) {
self.normalOverlay.alpha = self.translucencyAlphaNormal * self.alpha;
}
}
[self updateOverlayAlpha];
}

- (void)setInvertAlphaHighlighted:(CGFloat)invertAlphaHighlighted {
_invertAlphaHighlighted = invertAlphaHighlighted;
[self updateOverlayAlpha];
}

- (void)setTranslucencyAlphaNormal:(CGFloat)translucencyAlphaNormal {
_translucencyAlphaNormal = translucencyAlphaNormal;
[self updateOverlayAlpha];
}

- (void)setTranslucencyAlphaHighlighted:(CGFloat)translucencyAlphaHighlighted {
_translucencyAlphaHighlighted = translucencyAlphaHighlighted;
[self updateOverlayAlpha];
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
Expand Down Expand Up @@ -295,8 +316,13 @@ - (void)setVibrancyEffect:(UIVibrancyEffect *)vibrancyEffect {
#endif

- (void)setBackgroundColor:(UIColor *)backgroundColor {
self.normalOverlay.backgroundColor = backgroundColor;
self.highlightedOverlay.backgroundColor = backgroundColor;
NSLog(@"AYVibrantButton: backgroundColor is deprecated and has no effect. Use tintColor instead.");
[super setBackgroundColor:backgroundColor];
}

- (void)setTintColor:(UIColor *)tintColor {
self.normalOverlay.tintColor = tintColor;
self.highlightedOverlay.tintColor = tintColor;
}

- (void)setHideRightBorder:(BOOL)hideRightBorder {
Expand Down Expand Up @@ -341,8 +367,8 @@ - (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);

[self.backgroundColor setStroke];
[self.backgroundColor setFill];
[self.tintColor setStroke];
[self.tintColor setFill];

CGRect boxRect = CGRectInset(self.bounds, self.borderWidth / 2, self.borderWidth / 2);

Expand Down Expand Up @@ -400,7 +426,7 @@ - (void)drawRect:(CGRect)rect {
CGContextSetBlendMode(context, kCGBlendModeClear);
}

[self.text drawInRect:CGRectMake(0.0, (size.height - self.textHeight) / 2, size.width, self.textHeight) withAttributes:@{ NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.backgroundColor, NSParagraphStyleAttributeName:style }];
[self.text drawInRect:CGRectMake(0.0, (size.height - self.textHeight) / 2, size.width, self.textHeight) withAttributes:@{ NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.tintColor, NSParagraphStyleAttributeName:style }];
}
}

Expand All @@ -410,8 +436,8 @@ - (UIFont *)font {
return _font == nil ? [UIFont systemFontOfSize:kAYVibrantButtonDefaultFontSize] : _font;
}

- (UIColor *)backgroundColor {
return _backgroundColor == nil ? kAYVibrantButtonDefaultBackgroundColor : _backgroundColor;
- (UIColor *)tintColor {
return _tintColor == nil ? kAYVibrantButtonDefaultTintColor : _tintColor;
}

#pragma mark - Override Setters
Expand Down Expand Up @@ -451,7 +477,12 @@ - (void)setFont:(UIFont *)font {
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
_backgroundColor = backgroundColor;
NSLog(@"AYVibrantButtonOverlay: backgroundColor is deprecated and has no effect. Use tintColor instead.");
[super setBackgroundColor:backgroundColor];
}

- (void)setTintColor:(UIColor *)tintColor {
_tintColor = tintColor;
[self setNeedsDisplay];
}

Expand Down Expand Up @@ -530,6 +561,13 @@ - (void)setAnimationDuration:(CGFloat)animationDuration {
}
}

- (void)setInvertAlphaHighlighted:(CGFloat)invertAlphaHighlighted {
_invertAlphaHighlighted = invertAlphaHighlighted;
for (AYVibrantButton *button in self.buttons) {
button.invertAlphaHighlighted = invertAlphaHighlighted;
}
}

- (void)setTranslucencyAlphaNormal:(CGFloat)translucencyAlphaNormal {
_translucencyAlphaNormal = translucencyAlphaNormal;
for (AYVibrantButton *button in self.buttons) {
Expand Down Expand Up @@ -570,8 +608,13 @@ - (void)setVibrancyEffect:(UIVibrancyEffect *)vibrancyEffect {
#endif

- (void)setBackgroundColor:(UIColor *)backgroundColor {
_backgroundColor = backgroundColor;
[self.buttons makeObjectsPerformSelector:@selector(setBackgroundColor:) withObject:backgroundColor];
NSLog(@"AYVibrantButtonGroup: backgroundColor is deprecated and has no effect. Use tintColor instead.");
[super setBackgroundColor:backgroundColor];
}

- (void)setTintColor:(UIColor *)tintColor {
_tintColor = tintColor;
[self.buttons makeObjectsPerformSelector:@selector(setTintColor:) withObject:tintColor];
}

#pragma mark - Private Methods
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ button.backgroundColor = [UIColor blackColor];

## Changelog

### 1.0.4
* Added `invertAlphaHighlighted` property
* Changed the super class from `UIButton` to `UIControl`
* Changed `backgroundColor` to `tintColor`

### 1.0.3
* Added a new button style `AYVibrantButtonStyleFill`
* Fixed an issue that setting button alpha removes the vibrancy effect
Expand Down

0 comments on commit 93faa3d

Please sign in to comment.