Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support showing toast messages in different UIViews #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ALToastView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


@interface ALToastView : UIView {
Expand All @@ -32,5 +33,6 @@
}

+ (void)toastInView:(UIView *)parentView withText:(NSString *)text;
@property UIView *parentView;

@end
133 changes: 77 additions & 56 deletions ALToastView.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
static const CGFloat kDuration = 2;


// Static toastview queue variable
static NSMutableArray *toasts;
// Static dictionary with queue variables per each UIView
static NSMutableDictionary *dtoasts;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -59,13 +59,14 @@ @implementation ALToastView
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject

- (id)initWithText:(NSString *)text {
- (id)initWithText:(NSString *)text withView:(UIView*) pview {
if ((self = [self initWithFrame:CGRectZero])) {
// Add corner radius
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.6];
self.layer.cornerRadius = 5;
self.autoresizingMask = UIViewAutoresizingNone;
self.autoresizesSubviews = NO;
_parentView = pview;

// Init and add label
_textLabel = [[UILabel alloc] init];
Expand All @@ -85,82 +86,102 @@ - (id)initWithText:(NSString *)text {
}


- (void)dealloc {
[_textLabel release];

[super dealloc];
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public

+ (void)toastInView:(UIView *)parentView withText:(NSString *)text {
// Add new instance to queue
ALToastView *view = [[ALToastView alloc] initWithText:text];
ALToastView *view = [[ALToastView alloc] initWithText:text withView:parentView];

CGFloat lWidth = view.textLabel.frame.size.width;
CGFloat lHeight = view.textLabel.frame.size.height;
CGFloat pWidth = parentView.frame.size.width;
CGFloat pHeight = parentView.frame.size.height;
// Change toastview frame
view.frame = CGRectMake((pWidth - lWidth - 20) / 2., pHeight - lHeight - 60, lWidth + 20, lHeight + 10);

// Change toastview frame
view.frame = CGRectMake(((pWidth - lWidth - 20) / 2.), (pHeight - lHeight - 60), lWidth + 20, lHeight + 10);
view.alpha = 0.0f;

if (toasts == nil) {
toasts = [[NSMutableArray alloc] initWithCapacity:1];
[toasts addObject:view];
[ALToastView nextToastInView:parentView];
}
else {
[toasts addObject:view];
}

[view release];
NSString *key = [NSString stringWithFormat:@"%p", parentView];

if (dtoasts == nil) {
dtoasts = [[NSMutableDictionary alloc] initWithCapacity:0];
}
NSMutableArray *ltoasts;

@synchronized(dtoasts) {
ltoasts = [dtoasts objectForKey:key];
if(ltoasts == nil) {
ltoasts = [[NSMutableArray alloc] initWithCapacity:1];
[ltoasts addObject:view];
[dtoasts setObject:ltoasts forKey: key];
[ALToastView nextToastInView:parentView withViewList:ltoasts];
} else {
[ltoasts addObject:view];
}
}

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Private

- (void)fadeToastOut {
// Fade in parent view
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction

animations:^{
self.alpha = 0.f;
}
completion:^(BOOL finished){
UIView *parentView = self.superview;
[self removeFromSuperview];

// Remove current view from array
[toasts removeObject:self];
if ([toasts count] == 0) {
[toasts release];
toasts = nil;
// Fade in parent view
NSMutableArray *ltoasts;
NSString *key = [NSString stringWithFormat:@"%p", self.parentView];
ltoasts = [dtoasts objectForKey:key];

if(ltoasts == nil) {
NSLog(@"fadeToastOut: ltoast is nil");
return;
}

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction

animations:^{
self.alpha = 0.f;
}
else
[ALToastView nextToastInView:parentView];
}];
completion:^(BOOL finished){
UIView *parentView = self.superview;
[self removeFromSuperview];

@synchronized(dtoasts) {
NSMutableArray *ltoasts;
NSString *key = [NSString stringWithFormat:@"%p", self.parentView];
ltoasts = [dtoasts objectForKey:key];

if(ltoasts != nil) {
// Remove current view from array
[ltoasts removeObject:self];
if ([ltoasts count] == 0) {
[dtoasts removeObjectForKey:key];
}
else
[ALToastView nextToastInView:parentView withViewList:ltoasts];
}
}
}];
}


+ (void)nextToastInView:(UIView *)parentView {
if ([toasts count] > 0) {
ALToastView *view = [toasts objectAtIndex:0];

// Fade into parent view
[parentView addSubview:view];
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
view.alpha = 1.0;
} completion:^(BOOL finished){}];

// Start timer for fade out
[view performSelector:@selector(fadeToastOut) withObject:nil afterDelay:kDuration];
}
+ (void)nextToastInView:(UIView *)parentView withViewList:(NSMutableArray*) ltoasts{
@synchronized(dtoasts) {
if ([ltoasts count] > 0) {
ALToastView *view = [ltoasts objectAtIndex:0];

// Fade into parent view
[parentView addSubview:view];
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
view.alpha = 1.0;
} completion:^(BOOL finished){}];

// Start timer for fade out
[view performSelector:@selector(fadeToastOut) withObject:nil afterDelay:kDuration];
}
}
}

@end