From 358a9fbd28f91c7ad928bd2cfd3d733b0abded32 Mon Sep 17 00:00:00 2001 From: ReDetection Date: Tue, 11 Mar 2014 12:45:05 +0700 Subject: [PATCH 1/4] implemented changing text --- ALAlertBanner/ALAlertBanner+Private.h | 3 ++ ALAlertBanner/ALAlertBanner.h | 5 +++ ALAlertBanner/ALAlertBanner.m | 16 ++++++++ ALAlertBanner/ALAlertBannerManager.m | 53 +++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/ALAlertBanner/ALAlertBanner+Private.h b/ALAlertBanner/ALAlertBanner+Private.h index ca8e26a..7794559 100644 --- a/ALAlertBanner/ALAlertBanner+Private.h +++ b/ALAlertBanner/ALAlertBanner+Private.h @@ -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; @@ -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 animated:(BOOL)animated; - (id)nextAvailableViewController:(id)view; @end \ No newline at end of file diff --git a/ALAlertBanner/ALAlertBanner.h b/ALAlertBanner/ALAlertBanner.h index 5ac2e4b..b02d704 100755 --- a/ALAlertBanner/ALAlertBanner.h +++ b/ALAlertBanner/ALAlertBanner.h @@ -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. */ diff --git a/ALAlertBanner/ALAlertBanner.m b/ALAlertBanner/ALAlertBanner.m index c011d61..fc243fc 100755 --- a/ALAlertBanner/ALAlertBanner.m +++ b/ALAlertBanner/ALAlertBanner.m @@ -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 @@ -576,6 +580,15 @@ - (void)setInitialLayout { } } +- (void)setTitle:(NSString *)titleText subtitle:(NSString *)subtitleText animated:(BOOL)animated { + self.titleLabel.text = titleText; + self.subtitleLabel.text = subtitleText; + + CGFloat origHeight = self.bounds.size.height; + [self updateSizeAndSubviewsAnimated:animated]; + [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); @@ -587,6 +600,7 @@ - (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) { @@ -594,6 +608,7 @@ - (void)updateSizeAndSubviewsAnimated:(BOOL)animated { 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"]; } @@ -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"]; } diff --git a/ALAlertBanner/ALAlertBannerManager.m b/ALAlertBanner/ALAlertBannerManager.m index 7c65186..2bd2035 100755 --- a/ALAlertBanner/ALAlertBannerManager.m +++ b/ALAlertBanner/ALAlertBannerManager.m @@ -250,6 +250,59 @@ - (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 { + //todo check state + + 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 animated:YES]; + //TODO: move other alerts + + [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 From 120d341ea3a4ad47e6f9c72c6d81696df5b40e1a Mon Sep 17 00:00:00 2001 From: ReDetection Date: Tue, 11 Mar 2014 12:51:38 +0700 Subject: [PATCH 2/4] changed demo --- .../ALAlertBannerDemo/ViewController.m | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ALAlertBannerDemo/ALAlertBannerDemo/ViewController.m b/ALAlertBannerDemo/ALAlertBannerDemo/ViewController.m index d6df6bd..1363de1 100755 --- a/ALAlertBannerDemo/ALAlertBannerDemo/ViewController.m +++ b/ALAlertBannerDemo/ALAlertBannerDemo/ViewController.m @@ -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; @@ -26,6 +27,8 @@ @interface ViewController () @property (nonatomic) NSTimeInterval showAnimationDuration; @property (nonatomic) NSTimeInterval hideAnimationDuration; +@property (nonatomic, weak) ALAlertBanner *lastBanner; + @end @implementation ViewController @@ -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 { @@ -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 { @@ -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 { From a626bc4c555e876bee55bec95ed62efd41e0eda5 Mon Sep 17 00:00:00 2001 From: ReDetection Date: Tue, 11 Mar 2014 13:55:29 +0700 Subject: [PATCH 3/4] added .idea in the .gitignore and also added newline at the end of file --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9b8cb59..dafeeec 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ build/ xcuserdata *.xcuserstate *.xccheckout -*.moved-aside \ No newline at end of file +*.moved-aside +.idea From da4996130f2ce3f75ff844defdc2e6531cb21567 Mon Sep 17 00:00:00 2001 From: ReDetection Date: Tue, 11 Mar 2014 14:09:10 +0700 Subject: [PATCH 4/4] removed todo (implemented already) and removed unused method parameter --- ALAlertBanner/ALAlertBanner+Private.h | 2 +- ALAlertBanner/ALAlertBanner.m | 4 ++-- ALAlertBanner/ALAlertBannerManager.m | 5 +---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/ALAlertBanner/ALAlertBanner+Private.h b/ALAlertBanner/ALAlertBanner+Private.h index 7794559..0772c7a 100644 --- a/ALAlertBanner/ALAlertBanner+Private.h +++ b/ALAlertBanner/ALAlertBanner+Private.h @@ -66,7 +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 animated:(BOOL)animated; +- (void)setTitle:(NSString *)titleText subtitle:(NSString *)subtitleText; - (id)nextAvailableViewController:(id)view; @end \ No newline at end of file diff --git a/ALAlertBanner/ALAlertBanner.m b/ALAlertBanner/ALAlertBanner.m index fc243fc..9c7e793 100755 --- a/ALAlertBanner/ALAlertBanner.m +++ b/ALAlertBanner/ALAlertBanner.m @@ -580,12 +580,12 @@ - (void)setInitialLayout { } } -- (void)setTitle:(NSString *)titleText subtitle:(NSString *)subtitleText animated:(BOOL)animated { +- (void)setTitle:(NSString *)titleText subtitle:(NSString *)subtitleText { self.titleLabel.text = titleText; self.subtitleLabel.text = subtitleText; CGFloat origHeight = self.bounds.size.height; - [self updateSizeAndSubviewsAnimated:animated]; + [self updateSizeAndSubviewsAnimated:YES]; [self.delegate alertBanner:self willChangeHeight:origHeight toHeight:self.bounds.size.height inView:self.superview]; } diff --git a/ALAlertBanner/ALAlertBannerManager.m b/ALAlertBanner/ALAlertBannerManager.m index 2bd2035..1bf9022 100755 --- a/ALAlertBanner/ALAlertBannerManager.m +++ b/ALAlertBanner/ALAlertBannerManager.m @@ -272,8 +272,6 @@ - (void)alertBanner:(ALAlertBanner *)alertBanner willChangeHeight:(CGFloat)oldHe } - (void)alertBanner:(ALAlertBanner *)alertBanner changeTitle:(NSString *)title subtitle:(NSString *)subtitle hideAfter:(NSTimeInterval)delay { - //todo check state - dispatch_semaphore_t semaphore; switch (alertBanner.position) { case ALAlertBannerPositionTop: @@ -290,8 +288,7 @@ - (void)alertBanner:(ALAlertBanner *)alertBanner changeTitle:(NSString *)title s 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 animated:YES]; - //TODO: move other alerts + [alertBanner setTitle:title subtitle:subtitle]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideAlertBanner:) object:alertBanner]; if (delay > 0) {