Skip to content

Commit

Permalink
Refactor animations some more
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott90 committed Nov 30, 2013
1 parent 67f453b commit 6ed66ab
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions SDCAlertView/SDCAlertViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ @interface UIWindow (SDCAlertView)
+ (UIWindow *)sdc_alertWindow;
@end

@interface RBBSpringAnimation (SDCAlertView)
+ (RBBSpringAnimation *)sdc_alertViewSpringAnimationForKey:(NSString *)key;
@end


@interface SDCAlertView (SDCAlertViewPrivate)
@property (nonatomic, strong) SDCAlertViewBackgroundView *alertBackgroundView;
@property (nonatomic, strong) SDCAlertViewContentView *alertContentView;
Expand Down Expand Up @@ -122,7 +127,18 @@ - (void)showAlert:(SDCAlertView *)alert animated:(BOOL)animated {
[alert willBePresented];

if (animated) {
[self animateShowingAlert:alert completionHandler:^{
[self animateUsingBlock:^{
CATransform3D transformFrom = CATransform3DMakeScale(SDCAlertViewShowingAnimationScale, SDCAlertViewShowingAnimationScale, 1);
CATransform3D transformTo = CATransform3DMakeScale(1, 1, 1);
[self animateAlertTransform:alert from:transformFrom to:transformTo];

RBBSpringAnimation *opacityAnimation = [self animateAlertOpacity:alert from:0 to:1];

if ([self.alertViews count] == 1) {
self.backgroundColorView.layer.opacity = 1;
[self.backgroundColorView.layer addAnimation:opacityAnimation forKey:@"opacity"];
}
} completionHandler:^{
[alert didGetPresented];
}];
} else {
Expand All @@ -142,10 +158,22 @@ - (void)dismissAlert:(SDCAlertView *)alert animated:(BOOL)animated completion:(v
completionHandler();
};

if (animated)
[self animateHidingAlert:alert completionHandler:dismissBlock];
else
if (animated) {
[self animateUsingBlock:^{
CATransform3D transformFrom = CATransform3DMakeScale(1, 1, 1);
CATransform3D transformTo = CATransform3DMakeScale(SDCAlertViewDismissingAnimationScale, SDCAlertViewDismissingAnimationScale, 1);

[self animateAlertTransform:alert from:transformFrom to:transformTo];
RBBSpringAnimation *opacityAnimation = [self animateAlertOpacity:alert from:1 to:0];

if ([self.alertViews count] == 0) {
self.backgroundColorView.layer.opacity = 0;
[self.backgroundColorView.layer addAnimation:opacityAnimation forKey:@"opacity"];
}
} completionHandler:dismissBlock];
} else {
dismissBlock();
}
}

- (SDCAlertView *)currentAlert {
Expand All @@ -154,72 +182,49 @@ - (SDCAlertView *)currentAlert {

#pragma mark - Animations

- (void)animateShowingAlert:(SDCAlertView *)alert completionHandler:(void(^)(void))completionHandler {
[CATransaction begin];
[CATransaction setCompletionBlock:completionHandler];
[self applyAnimationsForShowingAlert:alert];
[CATransaction commit];
}

- (void)animateHidingAlert:(SDCAlertView *)alert completionHandler:(void(^)(void))completionHandler {
- (void)animateUsingBlock:(void(^)(void))animations completionHandler:(void(^)(void))completionHandler {
[CATransaction begin];
[CATransaction setCompletionBlock:completionHandler];
[self applyAnimationsForDismissingAlert:alert];
animations();
[CATransaction commit];
}

- (void)addTransformAnimationToAlert:(SDCAlertView *)alert transformingFrom:(CATransform3D)transformFrom to:(CATransform3D)transformTo {
RBBSpringAnimation *transformAnimation = [self springAnimationForKey:@"transform"];
- (RBBSpringAnimation *)animateAlertTransform:(SDCAlertView *)alert from:(CATransform3D)transformFrom to:(CATransform3D)transformTo {
RBBSpringAnimation *transformAnimation = [RBBSpringAnimation sdc_alertViewSpringAnimationForKey:@"transform"];
transformAnimation.fromValue = [NSValue valueWithCATransform3D:transformFrom];
transformAnimation.toValue = [NSValue valueWithCATransform3D:transformTo];

alert.layer.transform = transformTo;
[alert.layer addAnimation:transformAnimation forKey:@"transform"];
}

- (void)applyAnimationsForShowingAlert:(SDCAlertView *)alert {
CATransform3D transformFrom = CATransform3DMakeScale(SDCAlertViewShowingAnimationScale, SDCAlertViewShowingAnimationScale, 1);
CATransform3D transformTo = CATransform3DMakeScale(1, 1, 1);
[self addTransformAnimationToAlert:alert transformingFrom:transformFrom to:transformTo];

// Create opacity animation
RBBSpringAnimation *opacityAnimation = [self springAnimationForKey:@"opacity"];
opacityAnimation.fromValue = @0;
opacityAnimation.toValue = @1;
[alert.alertBackgroundView.layer addAnimation:opacityAnimation forKey:@"opacity"];
[alert.alertContentView.layer addAnimation:opacityAnimation forKey:@"opacity"];
[alert.toolbar.layer addAnimation:opacityAnimation forKey:@"opacity"];

// If we're animating the first alert in the queue, also animate the dimmed background
if ([self.alertViews count] == 1)
[self.backgroundColorView.layer addAnimation:opacityAnimation forKey:@"opacity"];
return transformAnimation;
}

- (void)applyAnimationsForDismissingAlert:(SDCAlertView *)alert {
CATransform3D transformFrom = CATransform3DMakeScale(1, 1, 1);
CATransform3D transformTo = CATransform3DMakeScale(SDCAlertViewDismissingAnimationScale, SDCAlertViewDismissingAnimationScale, 1);
[self addTransformAnimationToAlert:alert transformingFrom:transformFrom to:transformTo];
- (RBBSpringAnimation *)animateAlertOpacity:(SDCAlertView *)alert from:(CGFloat)fromValue to:(CGFloat)toValue {
RBBSpringAnimation *opacityAnimation = [RBBSpringAnimation sdc_alertViewSpringAnimationForKey:@"opacity"];
opacityAnimation.fromValue = @(fromValue);
opacityAnimation.toValue = @(toValue);

RBBSpringAnimation *opacityAnimation = [self springAnimationForKey:@"opacity"];
opacityAnimation.fromValue = @1;
opacityAnimation.toValue = @0;

alert.alertBackgroundView.layer.opacity = 0;
alert.alertContentView.layer.opacity = 0;
alert.toolbar.layer.opacity = 0;
alert.alertBackgroundView.layer.opacity = toValue;
alert.alertContentView.layer.opacity = toValue;
alert.toolbar.layer.opacity = toValue;

[alert.alertBackgroundView.layer addAnimation:opacityAnimation forKey:@"opacity"];
[alert.alertContentView.layer addAnimation:opacityAnimation forKey:@"opacity"];
[alert.toolbar.layer addAnimation:opacityAnimation forKey:@"opacity"];

return opacityAnimation;
}

// If the last alert is being dismissed, also animate the dimmed background back to normal
if ([self.alertViews count] == 1) {
self.backgroundColorView.layer.opacity = 0;
[self.backgroundColorView.layer addAnimation:opacityAnimation forKey:@"opacity"];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (RBBSpringAnimation *)springAnimationForKey:(NSString *)key {
@end

@implementation RBBSpringAnimation (SDCAlertView)

+ (RBBSpringAnimation *)sdc_alertViewSpringAnimationForKey:(NSString *)key {
RBBSpringAnimation *animation = [[RBBSpringAnimation alloc] init];
animation.duration = SDCAlertViewSpringAnimationDuration;
animation.damping = SDCAlertViewSpringAnimationDamping;
Expand All @@ -230,10 +235,6 @@ - (RBBSpringAnimation *)springAnimationForKey:(NSString *)key {
return animation;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

@implementation UIWindow(SDCAlertView)
Expand Down

0 comments on commit 6ed66ab

Please sign in to comment.