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

Adding Alert View Animation Options #60

Open
wants to merge 5 commits 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
1 change: 1 addition & 0 deletions BlockAlertsDemo/BlockAlertsDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (IBAction)showAlert:(id)sender
[alert addButtonWithTitle:@"Show another alert" imageIdentifier:@"green" block:^{
[self showAlert:nil];
}];
alert.direction = BlockAnimateFromTop;
[alert show];
}

Expand Down
11 changes: 11 additions & 0 deletions BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

#import <UIKit/UIKit.h>

@class BlockAlertView;

typedef enum {
BlockAnimateFromTop,
BlockAnimateFromBottom,
BlockAnimateFromLeft,
BlockAnimateFromRight,
BlockAnimateFadeIn
} BlockAnimationDirection;

@interface BlockAlertView : NSObject {
@protected
UIView *_view;
Expand Down Expand Up @@ -37,6 +47,7 @@

- (void)setupDisplay;

@property (nonatomic, readwrite) BlockAnimationDirection direction;
@property (nonatomic, retain) UIImage *backgroundImage;
@property (nonatomic, readonly) UIView *view;
@property (nonatomic, readwrite) BOOL vignetteBackground;
Expand Down
227 changes: 176 additions & 51 deletions BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ @implementation BlockAlertView
@synthesize view = _view;
@synthesize backgroundImage = _backgroundImage;
@synthesize vignetteBackground = _vignetteBackground;
@synthesize direction = _direction;

static UIImage *background = nil;
static UIImage *backgroundlandscape = nil;
Expand Down Expand Up @@ -139,7 +140,7 @@ - (void)setupDisplay
[self show];
}

- (id)initWithTitle:(NSString *)title message:(NSString *)message
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
self = [super init];

Expand All @@ -163,6 +164,8 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message
[self setupDisplay];

_vignetteBackground = NO;

_direction = BlockAnimateFromTop;
}

return self;
Expand Down Expand Up @@ -338,10 +341,35 @@ - (void)show
}
}


CGRect frame = _view.frame;
frame.origin.y = - _height;
frame.size.height = _height;
switch (_direction) {
case BlockAnimateFromTop:
frame.origin.y = - _height;
break;

case BlockAnimateFromBottom:
frame.origin.y = _height * 2;
break;

case BlockAnimateFromLeft:
frame.origin.y = floorf(([BlockBackground sharedInstance].bounds.size.height - _height) * 0.5);
frame.origin.x = -frame.size.width;
break;

case BlockAnimateFromRight:
frame.origin.y = floorf(([BlockBackground sharedInstance].bounds.size.height - _height) * 0.5);
frame.origin.x = frame.size.width * 2;
break;

case BlockAnimateFadeIn:
frame.origin.y = floorf(([BlockBackground sharedInstance].bounds.size.height - _height) * 0.5);
break;

default:
frame.origin.y = -_height;
break;
}
_view.frame = frame;

UIImageView *modalBackground = [[UIImageView alloc] initWithFrame:_view.bounds];
Expand All @@ -366,32 +394,44 @@ - (void)show
[BlockBackground sharedInstance].vignetteBackground = _vignetteBackground;
[[BlockBackground sharedInstance] addToMainWindow:_view];

__block CGPoint center = _view.center;
center.y = floorf([BlockBackground sharedInstance].bounds.size.height * 0.5) + kAlertViewBounce;

_cancelBounce = NO;

[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[BlockBackground sharedInstance].alpha = 1.0f;
_view.center = center;
}
completion:^(BOOL finished) {
if (_cancelBounce) return;

[UIView animateWithDuration:0.1
delay:0.0
options:0
animations:^{
center.y -= kAlertViewBounce;
_view.center = center;
}
completion:^(BOOL finished) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"AlertViewFinishedAnimations" object:self];
}];
}];
__block CGPoint firstPoint = _view.center;
__block CGPoint secondPoint = _view.center;

switch (_direction) {
case BlockAnimateFromTop:
firstPoint.y = floorf([BlockBackground sharedInstance].bounds.size.height * 0.5) + kAlertViewBounce;
secondPoint.y = firstPoint.y - kAlertViewBounce;
break;

case BlockAnimateFromBottom:
firstPoint.y = floorf([BlockBackground sharedInstance].bounds.size.height * 0.5) - kAlertViewBounce;
secondPoint.y = firstPoint.y + kAlertViewBounce;
break;

case BlockAnimateFromLeft:
firstPoint.x = floorf([BlockBackground sharedInstance].bounds.size.width * 0.5) + kAlertViewBounce;
secondPoint.x = firstPoint.x - kAlertViewBounce;
break;

case BlockAnimateFromRight:
firstPoint.x = floorf([BlockBackground sharedInstance].bounds.size.width * 0.5) - kAlertViewBounce;
secondPoint.x = firstPoint.x + kAlertViewBounce;
break;

case BlockAnimateFadeIn:
[self animateFadeIn];
break;

default:
firstPoint.y = floorf([BlockBackground sharedInstance].bounds.size.height * 0.5) - kAlertViewBounce;
secondPoint.y = firstPoint.y - kAlertViewBounce;
break;
}
if (_direction != BlockAnimateFadeIn) {
[self animateEnteringAlertTo:firstPoint thenTo:secondPoint];
}

[self retain];
}
Expand All @@ -413,30 +453,43 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim

if (animated)
{
[UIView animateWithDuration:0.1
delay:0.0
options:0
animations:^{
CGPoint center = _view.center;
center.y += 20;
_view.center = center;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
CGRect frame = _view.frame;
frame.origin.y = -frame.size.height;
_view.frame = frame;
[[BlockBackground sharedInstance] reduceAlphaIfEmpty];
}
completion:^(BOOL finished) {
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}];
}];
CGPoint firstPoint = _view.center;
CGPoint secondPoint = _view.center;
CGRect frame = _view.frame;

switch (_direction) {
case BlockAnimateFromTop:
firstPoint.y += kAlertViewBounce;
secondPoint.y = -frame.size.height - kAlertViewBounce;
break;

case BlockAnimateFromBottom:
firstPoint.y -= kAlertViewBounce;
secondPoint.y = (frame.size.height * 2) + kAlertViewBounce;
break;

case BlockAnimateFromLeft:
firstPoint.x += kAlertViewBounce;
secondPoint.x = -frame.size.width - kAlertViewBounce;
break;

case BlockAnimateFromRight:
firstPoint.x -= kAlertViewBounce;
secondPoint.x = (frame.size.width * 2) + kAlertViewBounce;
break;

case BlockAnimateFadeIn:
[self animateFadeOut];
break;

default:
firstPoint.y += kAlertViewBounce;
secondPoint.y = -frame.size.height - kAlertViewBounce;
break;
}
if (_direction != BlockAnimateFadeIn) {
[self animateExitingAlertTo:firstPoint thenTo:secondPoint];
}
}
else
{
Expand All @@ -446,6 +499,78 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Animations
- (void)animateEnteringAlertTo:(CGPoint)firstPoint thenTo:(CGPoint)secondPoint
{
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[BlockBackground sharedInstance].alpha = 1.0f;
_view.center = firstPoint;
}
completion:^(BOOL finished) {
if (_cancelBounce) return;

[UIView animateWithDuration:0.1
delay:0.0
options:0
animations:^{
_view.center = secondPoint;
}
completion:^(BOOL finished) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"AlertViewFinishedAnimations" object:self];
}];
}];
}

- (void)animateExitingAlertTo:(CGPoint)firstPoint thenTo:(CGPoint)secondPoint
{
[UIView animateWithDuration:0.1
delay:0.0
options:0
animations:^{
_view.center = firstPoint;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
_view.center = secondPoint;
[[BlockBackground sharedInstance] reduceAlphaIfEmpty];
}
completion:^(BOOL finished) {
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}];
}];
}

- (void)animateFadeIn
{
[UIView animateWithDuration:0.4 animations:^{
[BlockBackground sharedInstance].alpha = 1.0f;
}
completion:^(BOOL finished) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"AlertViewFinishedAnimations" object:self];
}];
}

- (void)animateFadeOut
{
[UIView animateWithDuration:0.4 animations:^{
[[BlockBackground sharedInstance] reduceAlphaIfEmpty];
}
completion:^(BOOL finished) {
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Action

Expand Down