Added new presentation and dismissal animation type

+ Push animation type allows alerts to slide in from/out to any screen edge.
This commit is contained in:
Warren Moore
2012-09-29 19:03:00 -05:00
parent 41fd294ca1
commit 0e7757e848
3 changed files with 96 additions and 19 deletions

View File

@@ -31,11 +31,26 @@ typedef enum {
AHAlertViewStyleLoginAndPasswordInput,
} AHAlertViewStyle;
typedef enum {
AHAlertViewEnterDirectionFromTop,
AHAlertViewEnterDirectionFromRight,
AHAlertViewEnterDirectionFromBottom,
AHAlertViewEnterDirectionFromLeft,
} AHAlertViewEnterDirection;
typedef enum {
AHAlertViewExitDirectionToTop,
AHAlertViewExitDirectionToRight,
AHAlertViewExitDirectionToBottom,
AHAlertViewExitDirectionToLeft,
} AHAlertViewExitDirection;
typedef enum {
AHAlertViewPresentationStyleNone = 0,
AHAlertViewPresentationStylePop,
AHAlertViewPresentationStyleFade,
AHAlertViewPresentationStylePush,
AHAlertViewPresentationStyleDefault = AHAlertViewPresentationStylePop
} AHAlertViewPresentationStyle;
@@ -45,6 +60,7 @@ typedef enum {
AHAlertViewDismissalStyleZoomOut,
AHAlertViewDismissalStyleFade,
AHAlertViewDismissalStyleTumble,
AHAlertViewDismissalStylePush,
AHAlertViewDismissalStyleDefault = AHAlertViewDismissalStyleFade
} AHAlertViewDismissalStyle;
@@ -65,6 +81,12 @@ typedef void (^AHAlertViewButtonBlock)();
@property(nonatomic, assign) AHAlertViewPresentationStyle presentationStyle;
// This property determines the animation used when the alert is dismissed.
@property(nonatomic, assign) AHAlertViewDismissalStyle dismissalStyle;
// For presentation animations that have an origin other than the center of the screen (push),
// this specifies the origination direction of the alert view.
@property(nonatomic, assign) AHAlertViewEnterDirection enterDirection;
// For dismissal animations that have an origin other than the center of the screen (push),
// this specifies the destination direction of the alert view.
@property(nonatomic, assign) AHAlertViewExitDirection exitDirection;
// Resets all UIAppearance modifiers back to generic iOS alert styles
+ (void)applySystemAlertAppearance;

View File

@@ -82,6 +82,7 @@ typedef void (^AHAnimationBlock)();
@property (nonatomic, strong) UIWindow *alertWindow;
@property (nonatomic, strong) UIWindow *previousKeyWindow;
@property (nonatomic, strong) UIImageView *dimView;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@@ -161,6 +162,8 @@ typedef void (^AHAnimationBlock)();
// Set default presentation and dismissal animation styles
_presentationStyle = AHAlertViewPresentationStyleDefault;
_dismissalStyle = AHAlertViewDismissalStyleDefault;
_enterDirection = AHAlertViewEnterDirectionFromRight;
_exitDirection = AHAlertViewExitDirectionToLeft;
// Subscribe to orientation and keyboard visibility change notifications
[[NSNotificationCenter defaultCenter] addObserver:self
@@ -422,8 +425,6 @@ typedef void (^AHAnimationBlock)();
// Cache the orientation we begin in.
previousOrientation = [[UIApplication sharedApplication] statusBarOrientation];
[self setNeedsLayout];
// Create a new alert-level UIWindow instance and make key. We need to do this so
// we appear above the status bar and can fade it appropriately.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
@@ -433,12 +434,14 @@ typedef void (^AHAnimationBlock)();
[self.alertWindow makeKeyAndVisible];
// Create a new radial gradiant background image to do the screen dimming effect
UIImageView *dimView = [[UIImageView alloc] initWithFrame:self.alertWindow.bounds];
dimView.image = [self backgroundGradientImageWithSize:self.alertWindow.bounds.size];
dimView.userInteractionEnabled = YES;
self.dimView = [[UIImageView alloc] initWithFrame:self.alertWindow.bounds];
self.dimView.image = [self backgroundGradientImageWithSize:self.alertWindow.bounds.size];
self.dimView.userInteractionEnabled = YES;
[self.alertWindow addSubview:dimView];
[dimView addSubview:self];
[self.alertWindow addSubview:self.dimView];
[self.alertWindow addSubview:self];
[self layoutIfNeeded];
// Animate the alert view itself onto the screen
[self performPresentationAnimation];
@@ -501,20 +504,40 @@ typedef void (^AHAnimationBlock)();
fadeInAnimation.duration = 0.3;
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1];
[self.superview.layer addAnimation:fadeInAnimation forKey:@"opacity"];
[self.dimView.layer addAnimation:fadeInAnimation forKey:@"opacity"];
}
else if(self.presentationStyle == AHAlertViewPresentationStyleFade)
{
// This presentation animation is a slightly more subtle presentation with a gentle fade in.
self.superview.alpha = 0;
self.dimView.alpha = self.alpha = 0;
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
self.superview.alpha = 1;
self.dimView.alpha = self.alpha = 1;
}
completion:nil];
}
else if(self.presentationStyle == AHAlertViewPresentationStylePush)
{
CGPoint targetCenter = self.center;
CGPoint offset = [self centerOffsetForDirection:self.enterDirection];
offset = CGPointApplyAffineTransform(offset, self.transform);
CGPoint originCenter = CGPointMake(self.center.x + offset.x, self.center.y + offset.y);
self.center = originCenter;
self.dimView.alpha = 0.01;
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
self.center = targetCenter;
self.dimView.alpha = 1;
}
completion:nil];
}
@@ -533,7 +556,7 @@ typedef void (^AHAnimationBlock)();
AHAnimationCompletionBlock completionBlock = ^(BOOL finished)
{
// Remove relevant views.
[self.superview removeFromSuperview];
[self.dimView removeFromSuperview];
[self removeFromSuperview];
// Restore previous key window and tear down our own window
@@ -558,7 +581,7 @@ typedef void (^AHAnimationBlock)();
offset = CGPointApplyAffineTransform(offset, self.transform);
self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeRotation(-M_PI_4));
self.center = CGPointMake(self.center.x + offset.x, self.center.y + offset.y);
self.superview.alpha = 0;
self.dimView.alpha = 0;
}
completion:completionBlock];
}
@@ -570,7 +593,7 @@ typedef void (^AHAnimationBlock)();
options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
self.superview.alpha = 0;
self.dimView.alpha = self.alpha = 0;
}
completion:completionBlock];
}
@@ -583,7 +606,7 @@ typedef void (^AHAnimationBlock)();
animations:^
{
self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeScale(0.01, 0.01));
self.superview.alpha = 0;
self.dimView.alpha = self.alpha = 0;
}
completion:completionBlock];
}
@@ -596,7 +619,22 @@ typedef void (^AHAnimationBlock)();
animations:^
{
self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeScale(10, 10));
self.superview.alpha = 0;
self.dimView.alpha = self.alpha = 0;
}
completion:completionBlock];
}
else if(self.dismissalStyle == AHAlertViewDismissalStylePush)
{
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
CGPoint offset = [self centerOffsetForDirection:self.exitDirection];
offset = CGPointApplyAffineTransform(offset, self.transform);
self.center = CGPointMake(self.center.x + offset.x, self.center.y + offset.y);
self.dimView.alpha = 0;
}
completion:completionBlock];
}
@@ -606,6 +644,23 @@ typedef void (^AHAnimationBlock)();
}
}
- (CGPoint)centerOffsetForDirection:(NSInteger)direction
{
switch(direction)
{
case AHAlertViewEnterDirectionFromTop: // also AHAlertViewExitDirectionToTop
return CGPointMake(0, -self.superview.bounds.size.height * 1.5);
case AHAlertViewEnterDirectionFromRight: // also AHAlertViewExitDirectionToRight
return CGPointMake(self.superview.bounds.size.width * 1.5, 0);
case AHAlertViewEnterDirectionFromBottom: // also AHAlertViewExitDirectionToBottom
return CGPointMake(0, self.superview.bounds.size.height * 1.5);
case AHAlertViewEnterDirectionFromLeft: // also AHAlertViewExitDirectionToLeft
return CGPointMake(-self.superview.bounds.size.width * 1.5, 0);
}
return CGPointZero;
}
#pragma mark - Layout calculation methods
- (void)layoutSubviews {

View File

@@ -99,10 +99,10 @@ static const NSInteger kAHViewAutoresizingFlexibleMargins =
NSString *message = @"This is a message that might prompt you to do something.";
AHAlertView *alert = [[AHAlertView alloc] initWithTitle:title message:message];
[alert setCancelButtonTitle:@"Cancel" block:^{
[alert setCancelButtonTitle:@"Tumble" block:^{
alert.dismissalStyle = AHAlertViewDismissalStyleTumble;
}];
[alert addButtonWithTitle:@"OK" block:^{
[alert addButtonWithTitle:@"Zoom Down" block:^{
alert.dismissalStyle = AHAlertViewDismissalStyleZoomDown;
}];
[alert show];