Skip to content

Commit

Permalink
Merge pull request #12 from vokal/master
Browse files Browse the repository at this point in the history
Change count to NSUInteger, make local constants static const
  • Loading branch information
cwRichardKim committed May 20, 2015
2 parents 521cd49 + 43477a6 commit ff4b3e4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Code:
```
``` objc
-(void)increment;
-(void)incrementBy:(int)amount;
-(void)incrementBy:(NSUInteger)amount;
-(void)decrement;
-(void)decrementBy:(int)amount;
-(void)setCount:(int)newCount; //%%% set to a certain number
-(void)decrementBy:(NSUInteger)amount;
-(void)setCount:(NSUInteger)newCount; //%%% set to a certain number
```

__Combine Actions!__
Expand Down Expand Up @@ -73,7 +73,6 @@ __Combine Actions!__

**It isn't incrementing / decrementing properly!**
* I've written it so that any count < 1 doesn't show up. If you need help customizing this, reach out to me
* Calling [decrement] will never bring the count below 0, but calling [decrementBy:] or [setCount:] will allow negative values (negative values still won't show up)

**The circle is in a weird place**
* If you want to resize the circle, use [scaleCircleSizeBy:]. 0.5 will give you half the size, 2 will give you double
Expand All @@ -91,6 +90,7 @@ __Combine Actions!__
* 1.0.1 cocoapod allows iOS 7.0
* 1.0.2 added "hideCount", "showCount", and "count" methods, allowing indeterminate badges with no number
* 1.0.5 added bubble expansion for larger numbers [(gif)](http://i.imgur.com/cpQuShT.gif)
* 2.0.0 changed count to `NSUInteger` (removed support for negative counts), made local constants `static const`

### Areas for Improvements / involvement
* A mechanism for adding a custom animation
Expand Down
2 changes: 1 addition & 1 deletion RKNotificationHub.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "RKNotificationHub"
s.version = "1.0.5"
s.version = "2.0.0"
s.summary = "iOS: Make any UIView a full fledged notification center"

s.description = "A simple one line solution to turning adding a notification bubble to any UIView. Easily increment, decrement, an animate the notification, -Notification, -Notification Center, -objectivec, -ios, -iphone, -xcode"
Expand Down
16 changes: 10 additions & 6 deletions RKNotificationHub/RKNotificationHub.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,31 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/**
* The default diameter of the notification hub view.
*/
FOUNDATION_EXPORT CGFloat const RKNotificationHubDefaultDiameter;

@interface RKNotificationHub : NSObject

//%%% setup
- (id)initWithView:(UIView *)view;
- (id)initWithBarButtonItem:(UIBarButtonItem *)barButtonItem;

//%%% adjustment methods
- (void)setView:(UIView *)view andCount:(int)startCount;
- (void)setView:(UIView *)view andCount:(NSUInteger)startCount;
- (void)setCircleAtFrame:(CGRect)frame;
- (void)setCircleColor:(UIColor*)circleColor labelColor:(UIColor*)labelColor;
- (void)moveCircleByX:(CGFloat)x Y:(CGFloat)y;
- (void)scaleCircleSizeBy:(CGFloat)scale;
- (void)setCountLabelFont:(UIFont *)font;
@property (nonatomic, strong) UIFont *countLabelFont;

//%%% changing the count
- (void)increment;
- (void)incrementBy:(int)amount;
- (void)incrementBy:(NSUInteger)amount;
- (void)decrement;
- (void)decrementBy:(int)amount;
- (void)setCount:(int)newCount;
- (int)count; // returns the count (treat as get method)
- (void)decrementBy:(NSUInteger)amount;
@property (nonatomic, assign) NSUInteger count;

//%%% hiding / showing the count
- (void)hideCount;
Expand Down
68 changes: 34 additions & 34 deletions RKNotificationHub/RKNotificationHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
#import <QuartzCore/QuartzCore.h>

//%%% default diameter
CGFloat kDefaultDiameter = 30;
CGFloat kCountMagnitudeAdaptationRatio = 0.3;
CGFloat const RKNotificationHubDefaultDiameter = 30;
static CGFloat const kCountMagnitudeAdaptationRatio = 0.3;
//%%% pop values
CGFloat kPopStartRatio = .85;
CGFloat kPopOutRatio = 1.05;
CGFloat kPopInRatio = .95;
static CGFloat const kPopStartRatio = .85;
static CGFloat const kPopOutRatio = 1.05;
static CGFloat const kPopInRatio = .95;

//%%% blink values
CGFloat kBlinkDuration = 0.1;
CGFloat kBlinkAlpha = 0.1;
static CGFloat const kBlinkDuration = 0.1;
static CGFloat const kBlinkAlpha = 0.1;

//%%% bump values
CGFloat kFirstBumpDistance = 8.0;
CGFloat kBumpTimeSeconds = 0.13;
CGFloat SECOND_BUMP_DIST = 4.0;
CGFloat kBumpTimeSeconds2 = 0.1;
static CGFloat const kFirstBumpDistance = 8.0;
static CGFloat const kBumpTimeSeconds = 0.13;
static CGFloat const SECOND_BUMP_DIST = 4.0;
static CGFloat const kBumpTimeSeconds2 = 0.1;

@interface RKView : UIView
@property (nonatomic) BOOL isUserChangingBackgroundColor;
Expand All @@ -45,7 +45,6 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor


@implementation RKNotificationHub {
int count;
int curOrderMagnitude;
UILabel *countLabel;
RKView *redCircle;
Expand Down Expand Up @@ -80,7 +79,7 @@ - (id)initWithBarButtonItem:(UIBarButtonItem *)barButtonItem

//%%% give this a view and an initial count (0 hides the notification circle)
// and it will make a hub for you
- (void)setView:(UIView *)view andCount:(int)startCount
- (void)setView:(UIView *)view andCount:(NSUInteger)startCount
{
curOrderMagnitude = 0;

Expand All @@ -95,11 +94,11 @@ - (void)setView:(UIView *)view andCount:(int)startCount

countLabel = [[UILabel alloc]initWithFrame:redCircle.frame];
countLabel.userInteractionEnabled = NO;
[self setCount:startCount];
self.count = startCount;
[countLabel setTextAlignment:NSTextAlignmentCenter];
countLabel.textColor = [UIColor whiteColor];

[self setCircleAtFrame:CGRectMake(frame.size.width- (kDefaultDiameter*2/3), -kDefaultDiameter/3, kDefaultDiameter, kDefaultDiameter)];
[self setCircleAtFrame:CGRectMake(frame.size.width- (RKNotificationHubDefaultDiameter*2/3), -RKNotificationHubDefaultDiameter/3, RKNotificationHubDefaultDiameter, RKNotificationHubDefaultDiameter)];

[view addSubview:redCircle];
[view addSubview:countLabel];
Expand Down Expand Up @@ -169,50 +168,51 @@ - (void)showCount
//%%% increases count by 1
- (void)increment
{
[self setCount:count+1];
[self incrementBy:1];
}

//%%% increases count by amount
- (void)incrementBy:(int)amount
- (void)incrementBy:(NSUInteger)amount
{
[self setCount:count+amount];
self.count += amount;
}

//%%% decreases count
- (void)decrement
{
if (count == 0) {
return;
}
[self setCount:count-1];
[self decrementBy:1];
}

//%%% decreases count by amount
- (void)decrementBy:(int)amount
- (void)decrementBy:(NSUInteger)amount
{
[self setCount:count-amount];
if (amount >= self.count) {
self.count = 0;
return;
}
self.count -= amount;
}

//%%% set the count yourself
- (void)setCount:(int)newCount
- (void)setCount:(NSUInteger)newCount
{
count = newCount;
countLabel.text = [NSString stringWithFormat:@"%i",count];
_count = newCount;
countLabel.text = [NSString stringWithFormat:@"%@", @(self.count)];
[self checkZero];
[self expandToFitLargerDigits];
}

- (int)count
{
return count;
}

//%% set the font of the label
- (void)setCountLabelFont:(UIFont *)font
{
[countLabel setFont:[UIFont fontWithName:font.fontName size:redCircle.frame.size.width/2]];
}

- (UIFont *)countLabelFont
{
return countLabel.font;
}

#pragma mark - ANIMATION

//%%% animation that resembles facebook's pop
Expand Down Expand Up @@ -367,7 +367,7 @@ - (void)setAlpha:(float)alpha
//%%% hides the notification if the value is 0
- (void)checkZero
{
if (count <= 0) {
if (self.count <= 0) {
redCircle.hidden = YES;
countLabel.hidden = YES;
} else {
Expand All @@ -379,7 +379,7 @@ - (void)checkZero
}

- (void)expandToFitLargerDigits {
int orderOfMagnitude = log10((double)count);
int orderOfMagnitude = log10((double)self.count);
orderOfMagnitude = (orderOfMagnitude >= 2) ? orderOfMagnitude : 1;
CGRect frame = initialFrame;
frame.size.width = initialFrame.size.width * (1 + kCountMagnitudeAdaptationRatio * (orderOfMagnitude - 1));
Expand Down

0 comments on commit ff4b3e4

Please sign in to comment.