Skip to content

Commit

Permalink
Adding new init-with-view method, bumped version number.
Browse files Browse the repository at this point in the history
  • Loading branch information
kreeger committed Dec 17, 2012
1 parent df00b21 commit 403900e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions BDKNotifyHUD.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'BDKNotifyHUD'
s.version = '1.0.0'
s.version = '1.1.0'
s.platform = :ios
s.summary = 'An animated UIView for displaying a temporary "bezel" notification, mid-screen.'
s.description = 'An animated UIView for displaying a temporary "bezel" notification, mid-screen.'
Expand All @@ -18,7 +18,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
LICENSE
}
s.author = { 'Benjamin Kreeger' => 'http://ben.kree.gr/' }
s.source = { :git => 'https://github.com/kreeger/BDKNotifyHUD.git', :tag => 'v1.0.0' }
s.source = { :git => 'https://github.com/kreeger/BDKNotifyHUD.git', :tag => 'v1.1.0' }
s.source_files = 'BDKNotifyHUD/BDKNotifyHUD.{h,m}'
s.frameworks = 'QuartzCore'
s.requires_arc = true
Expand Down
6 changes: 5 additions & 1 deletion BDKNotifyHUD/BDKNotifyHUD.h
Expand Up @@ -7,17 +7,21 @@

@property (nonatomic) CGFloat destinationOpacity;
@property (nonatomic) CGFloat currentOpacity;
@property (nonatomic) UIImage *image;
@property (nonatomic) UIView *iconView;
@property (nonatomic) CGFloat roundness;
@property (nonatomic) BOOL bordered;
@property (nonatomic) BOOL isAnimating;

@property (strong, nonatomic) UIColor *borderColor;
@property (strong, nonatomic) NSString *text;

+ (id)notifyHUDWithView:(UIView *)view text:(NSString *)text;
+ (id)notifyHUDWithImage:(UIImage *)image text:(NSString *)text;

- (id)initWithView:(UIView *)view text:(NSString *)text;
- (id)initWithImage:(UIImage *)image text:(NSString *)text;

- (void)setImage:(UIImage *)image;
- (void)presentWithDuration:(CGFloat)duration speed:(CGFloat)speed inView:(UIView *)view completion:(void (^)(void))completion;

@end
73 changes: 41 additions & 32 deletions BDKNotifyHUD/BDKNotifyHUD.m
@@ -1,4 +1,5 @@
#import "BDKNotifyHUD.h"

#import <QuartzCore/QuartzCore.h>

#define kBDKNotifyHUDDefaultRoundness 10.0f
Expand All @@ -22,9 +23,9 @@ - (void)fireBlockAfterDelay:(void (^)(void))block {
@interface BDKNotifyHUD ()

@property (strong, nonatomic) UIView *backgroundView;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *textLabel;

- (void)setupView;
- (void)recalculateHeight;
- (void)adjustTextLabel:(UILabel *)label;
- (void)fadeAfter:(CGFloat)duration speed:(CGFloat)speed completion:(void (^)(void))completion;
Expand All @@ -35,6 +36,10 @@ @implementation BDKNotifyHUD

#pragma mark - Lifecycle

+ (id)notifyHUDWithView:(UIView *)view text:(NSString *)text {
return [[self alloc] initWithView:view text:text];
}

+ (id)notifyHUDWithImage:(UIImage *)image text:(NSString *)text {
return [[self alloc] initWithImage:image text:text];
}
Expand All @@ -43,22 +48,35 @@ + (CGRect)defaultFrame {
return CGRectMake(0, 0, kBDKNotifyHUDDefaultWidth, kBDKNotifyHUDDefaultHeight);
}

- (id)initWithView:(UIView *)view text:(NSString *)text {
if ((self = [self initWithFrame:[self.class defaultFrame]])) {
self.iconView = view;
self.text = text;
[self setupView];
}
return self;
}

- (id)initWithImage:(UIImage *)image text:(NSString *)text {
if ((self = [self initWithFrame:[self.class defaultFrame]])) {
self.image = image;
[self setImage:image];
self.text = text;
self.roundness = kBDKNotifyHUDDefaultRoundness;
self.borderColor = [UIColor clearColor];
self.destinationOpacity = kBDKNotifyHUDDefaultOpacity;
self.currentOpacity = 0.0f;
[self addSubview:self.backgroundView];
[self addSubview:self.imageView];
[self addSubview:self.textLabel];
[self recalculateHeight];
[self setupView];
}
return self;
}

- (void)setupView {
self.roundness = kBDKNotifyHUDDefaultRoundness;
self.borderColor = [UIColor clearColor];
self.destinationOpacity = kBDKNotifyHUDDefaultOpacity;
self.currentOpacity = 0.0f;
[self addSubview:self.backgroundView];
[self addSubview:self.iconView];
[self addSubview:self.textLabel];
[self recalculateHeight];
}

- (void)presentWithDuration:(CGFloat)duration speed:(CGFloat)speed inView:(UIView *)view completion:(void (^)(void))completion {
self.isAnimating = YES;
[UIView animateWithDuration:speed animations:^{
Expand Down Expand Up @@ -102,12 +120,21 @@ - (void)setText:(NSString *)text {
}

- (void)setImage:(UIImage *)image {
if (_imageView != nil) self.imageView.image = image;
_image = image;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.backgroundColor = [UIColor clearColor];
imageView.contentMode = UIViewContentModeCenter;
imageView.image = image;
CGRect frame = imageView.frame;
frame.size = image.size;
frame.origin = CGPointMake((self.backgroundView.frame.size.width - frame.size.width) / 2, kBDKNotifyHUDDefaultPadding);
imageView.frame = frame;
imageView.alpha = 0.0f;

_iconView = imageView;
}

- (void)setCurrentOpacity:(CGFloat)currentOpacity {
self.imageView.alpha = currentOpacity > 0 ? 1.0f : 0.0f;
self.iconView.alpha = currentOpacity > 0 ? 1.0f : 0.0f;
self.textLabel.alpha = currentOpacity > 0 ? 1.0f : 0.0f;
self.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:currentOpacity];
_currentOpacity = currentOpacity;
Expand All @@ -127,28 +154,10 @@ - (UIView *)backgroundView {
return _backgroundView;
}

- (UIImageView *)imageView {
if (_imageView != nil) return _imageView;

_imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_imageView.backgroundColor = [UIColor clearColor];
_imageView.contentMode = UIViewContentModeCenter;
if (self.image != nil) {
_imageView.image = self.image;
CGRect frame = _imageView.frame;
frame.size = self.image.size;
frame.origin = CGPointMake((self.backgroundView.frame.size.width - frame.size.width) / 2, kBDKNotifyHUDDefaultPadding);
_imageView.frame = frame;
_imageView.alpha = 0.0f;
}

return _imageView;
}

- (UILabel *)textLabel {
if (_textLabel != nil) return _textLabel;

CGRect frame = CGRectMake(0, floorf(CGRectGetMaxY(self.imageView.frame) + kBDKNotifyHUDDefaultInnerPadding),
CGRect frame = CGRectMake(0, floorf(CGRectGetMaxY(self.iconView.frame) + kBDKNotifyHUDDefaultInnerPadding),
floorf(self.backgroundView.frame.size.width),
floorf(self.backgroundView.frame.size.height / 2.0f));
_textLabel = [[UILabel alloc] initWithFrame:frame];
Expand Down
6 changes: 3 additions & 3 deletions readme.markdown
Expand Up @@ -7,9 +7,9 @@ Displays a translucent-ish modal over with an image and a label; largely customi
## Usage

``` objective-c
// Create the HUD object
BDKNotifyHUD *hud = [BDKNotifyHUD notifyHUDWithImage:[UIImage imageNamed:@"Checkmark.png"]
text:@"This is a checkmark!"];
// Create the HUD object; view can be a UIImageView, an icon... you name it
BDKNotifyHUD *hud = [BDKNotifyHUD notifyHUDWithView:[UIView someCustomView]
text:@"This is a checkmark!"];
hud.center = CGPointMake(self.view.center.x, self.view.center.y - 20);

// Animate it, then get rid of it. These settings last 1 second, takes a half-second fade.
Expand Down

0 comments on commit 403900e

Please sign in to comment.