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 for title and subtitle changing #28

Open
wants to merge 4 commits into
base: main
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ build/
xcuserdata
*.xcuserstate
*.xccheckout
*.moved-aside
*.moved-aside
.idea
3 changes: 3 additions & 0 deletions ALAlertBanner/ALAlertBanner+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
@required
- (void)showAlertBanner:(ALAlertBanner *)alertBanner hideAfter:(NSTimeInterval)delay;
- (void)hideAlertBanner:(ALAlertBanner *)alertBanner forced:(BOOL)forced;
- (void)alertBanner:(ALAlertBanner *)alertBanner changeTitle:(NSString *)title subtitle:(NSString *)subtitle hideAfter:(NSTimeInterval)delay;
- (void)alertBanner:(ALAlertBanner *)alertBanner willChangeHeight:(CGFloat)height toHeight:(CGFloat)newHeight inView:(UIView *)view;
- (void)alertBannerWillShow:(ALAlertBanner *)alertBanner inView:(UIView *)view;
- (void)alertBannerDidShow:(ALAlertBanner *)alertBanner inView:(UIView *)view;
- (void)alertBannerWillHide:(ALAlertBanner *)alertBanner inView:(UIView *)view;
Expand All @@ -64,6 +66,7 @@
- (void)pushAlertBanner:(CGFloat)distance forward:(BOOL)forward delay:(double)delay;
- (void)updateSizeAndSubviewsAnimated:(BOOL)animated;
- (void)updatePositionAfterRotationWithY:(CGFloat)yPos animated:(BOOL)animated;
- (void)setTitle:(NSString *)titleText subtitle:(NSString *)subtitleText;
- (id)nextAvailableViewController:(id)view;

@end
5 changes: 5 additions & 0 deletions ALAlertBanner/ALAlertBanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ typedef enum {
*/
- (void)hide;

/**
Change title and delay hiding.
*/
- (void)changeTitle:(NSString *)title subtitle:(NSString *)subtitle;

/**
Returns an array of all banners within a certain view.
*/
Expand Down
16 changes: 16 additions & 0 deletions ALAlertBanner/ALAlertBanner.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ - (void)hide {
[self.delegate hideAlertBanner:self forced:NO];
}

- (void)changeTitle:(NSString *)title subtitle:(NSString *)subtitle {
[self.delegate alertBanner:self changeTitle:title subtitle:subtitle hideAfter:self.secondsToShow];
}

# pragma mark -
# pragma mark Internal Instance Methods

Expand Down Expand Up @@ -576,6 +580,15 @@ - (void)setInitialLayout {
}
}

- (void)setTitle:(NSString *)titleText subtitle:(NSString *)subtitleText {
self.titleLabel.text = titleText;
self.subtitleLabel.text = subtitleText;

CGFloat origHeight = self.bounds.size.height;
[self updateSizeAndSubviewsAnimated:YES];
[self.delegate alertBanner:self willChangeHeight:origHeight toHeight:self.bounds.size.height inView:self.superview];
}

- (void)updateSizeAndSubviewsAnimated:(BOOL)animated {
CGSize maxLabelSize = CGSizeMake(self.superview.bounds.size.width - (kMargin*3.f) - self.styleImageView.image.size.width, CGFLOAT_MAX);
CGFloat titleLabelHeight = AL_SINGLELINE_TEXT_HEIGHT(self.titleLabel.text, self.titleLabel.font);
Expand All @@ -587,13 +600,15 @@ - (void)updateSizeAndSubviewsAnimated:(BOOL)animated {
CGRect oldBounds = self.layer.bounds;
CGRect newBounds = oldBounds;
newBounds.size = CGSizeMake(self.superview.frame.size.width, heightForSelf);
BOOL forward = newBounds.size.height > oldBounds.size.height;
self.layer.bounds = newBounds;

if (animated) {
CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
boundsAnimation.fromValue = [NSValue valueWithCGRect:oldBounds];
boundsAnimation.toValue = [NSValue valueWithCGRect:newBounds];
boundsAnimation.duration = boundsAnimationDuration;
boundsAnimation.timingFunction = forward ? [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut] : [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.layer addAnimation:boundsAnimation forKey:@"bounds"];
}

Expand All @@ -619,6 +634,7 @@ - (void)updateSizeAndSubviewsAnimated:(BOOL)animated {
CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
shadowAnimation.fromValue = (id)[UIBezierPath bezierPathWithRect:oldShadowPath].CGPath;
shadowAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:newShadowPath].CGPath;
shadowAnimation.timingFunction = forward ? [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut] : [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
shadowAnimation.duration = boundsAnimationDuration;
[self.layer addAnimation:shadowAnimation forKey:@"shadowPath"];
}
Expand Down
50 changes: 50 additions & 0 deletions ALAlertBanner/ALAlertBannerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,56 @@ - (void)alertBannerDidHide:(ALAlertBanner *)alertBanner inView:(UIView *)view {
}
}

- (void)alertBanner:(ALAlertBanner *)alertBanner willChangeHeight:(CGFloat)oldHeight toHeight:(CGFloat)newHeight inView:(UIView *)view {
NSMutableArray *bannersArray = view.alertBanners;
NSArray *bannersInSamePosition = [bannersArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.position == %i", alertBanner.position]];
NSUInteger index = [bannersInSamePosition indexOfObject:alertBanner];

BOOL forward = newHeight > oldHeight;
CGFloat distance = newHeight - oldHeight;

if (index != NSNotFound && index > 0) {
NSArray *bannersToPush = [bannersInSamePosition subarrayWithRange:NSMakeRange(0, index)];

for (ALAlertBanner *banner in bannersToPush)
[banner pushAlertBanner:distance forward:forward delay:0.f];
}

if (alertBanner.position == ALAlertBannerPositionBottom) {
[alertBanner pushAlertBanner:distance forward:forward delay:0.f];
}

}

- (void)alertBanner:(ALAlertBanner *)alertBanner changeTitle:(NSString *)title subtitle:(NSString *)subtitle hideAfter:(NSTimeInterval)delay {
dispatch_semaphore_t semaphore;
switch (alertBanner.position) {
case ALAlertBannerPositionTop:
semaphore = self.topPositionSemaphore;
break;
case ALAlertBannerPositionBottom:
semaphore = self.bottomPositionSemaphore;
break;
case ALAlertBannerPositionUnderNavBar:
semaphore = self.navBarPositionSemaphore;
break;
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
[alertBanner setTitle:title subtitle:subtitle];

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideAlertBanner:) object:alertBanner];
if (delay > 0) {
[self performSelector:@selector(hideAlertBanner:) withObject:alertBanner afterDelay:delay];
}

dispatch_semaphore_signal(semaphore);
});
});
}

# pragma mark -
# pragma mark Instance Methods

Expand Down
16 changes: 16 additions & 0 deletions ALAlertBannerDemo/ALAlertBannerDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @interface ViewController ()
@property (nonatomic, strong) UIButton *topButton;
@property (nonatomic, strong) UIButton *bottomButton;
@property (nonatomic, strong) UIButton *underNavButton;
@property (nonatomic, strong) UIButton *changeTextButton;

@property (nonatomic, strong) UISlider *secondsToShowSlider;
@property (nonatomic, strong) UILabel *secondsToShowLabel;
Expand All @@ -26,6 +27,8 @@ @interface ViewController ()
@property (nonatomic) NSTimeInterval showAnimationDuration;
@property (nonatomic) NSTimeInterval hideAnimationDuration;

@property (nonatomic, weak) ALAlertBanner *lastBanner;

@end

@implementation ViewController
Expand Down Expand Up @@ -100,6 +103,11 @@ - (void)viewDidLoad {
self.animationDurationLabel.text = @"Animation duration: 0.25 seconds";
self.animationDurationLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.animationDurationLabel];

self.changeTextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.changeTextButton setTitle:@"Change text" forState:UIControlStateNormal];
[self.changeTextButton addTarget:self action:@selector(changeText) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.changeTextButton];
}

- (void)viewWillAppear:(BOOL)animated {
Expand All @@ -121,6 +129,8 @@ - (void)configureView {
self.secondsToShowLabel.frame = CGRectMake(self.secondsToShowSlider.frame.origin.x, self.secondsToShowSlider.frame.origin.y + self.secondsToShowSlider.frame.size.height, self.secondsToShowSlider.frame.size.width, 20.f);
self.animationDurationSlider.frame = CGRectMake(self.secondsToShowSlider.frame.origin.x, self.secondsToShowLabel.frame.origin.y + self.secondsToShowLabel.frame.size.height + 20.f, self.view.frame.size.width - 40.f, 20.f);
self.animationDurationLabel.frame = CGRectMake(self.animationDurationSlider.frame.origin.x, self.animationDurationSlider.frame.origin.y + self.animationDurationSlider.frame.size.height, self.animationDurationSlider.frame.size.width, 20.f);

self.changeTextButton.frame = CGRectMake(self.bottomButton.frame.origin.x, self.animationDurationSlider.frame.origin.y + self.animationDurationSlider.frame.size.height + 20.f, self.bottomButton.frame.size.width, self.bottomButton.frame.size.height);
}

- (void)showAlertBannerInView:(UIButton *)button {
Expand All @@ -134,6 +144,12 @@ - (void)showAlertBannerInView:(UIButton *)button {
banner.showAnimationDuration = self.showAnimationDuration;
banner.hideAnimationDuration = self.hideAnimationDuration;
[banner show];

self.lastBanner = banner;
}

- (void)changeText {
[self.lastBanner changeTitle:@"Text changed" subtitle:[[AppDelegate randomLoremIpsum] stringByAppendingString:[AppDelegate randomLoremIpsum]]];
}

- (void)showAlertBannerInWindow:(UIButton *)button {
Expand Down