Skip to content

Commit 04711f6

Browse files
committed
Merge branch 'feature/background' into develop
2 parents a9f98e9 + 18d803f commit 04711f6

3 files changed

Lines changed: 77 additions & 53 deletions

File tree

SIAlertView/SIAlertView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ typedef NS_ENUM(NSInteger, SIAlertViewButtonType) {
1414
SIAlertViewButtonTypeCancel
1515
};
1616

17+
typedef NS_ENUM(NSInteger, SIAlertViewBackgroundStyle) {
18+
SIAlertViewBackgroundStyleGradient = 0,
19+
SIAlertViewBackgroundStyleSolid,
20+
};
21+
1722
typedef NS_ENUM(NSInteger, SIAlertViewTransitionStyle) {
1823
SIAlertViewTransitionStyleSlideFromBottom = 0,
1924
SIAlertViewTransitionStyleSlideFromTop,
@@ -31,6 +36,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
3136
@property (nonatomic, copy) NSString *message;
3237

3338
@property (nonatomic, assign) SIAlertViewTransitionStyle transitionStyle; // default is SIAlertViewTransitionStyleSlideFromBottom
39+
@property (nonatomic, assign) SIAlertViewBackgroundStyle backgroundStyle; // default is SIAlertViewButtonTypeGradient
3440

3541
@property (nonatomic, copy) SIAlertViewHandler willShowHandler;
3642
@property (nonatomic, copy) SIAlertViewHandler didShowHandler;

SIAlertView/SIAlertView.m

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,69 +43,70 @@ @interface SIAlertView () <SIAlertViewControllerDelegate>
4343

4444
@property (nonatomic, assign, getter = isVisible) BOOL visible;
4545

46-
+ (SIAlertBackgroundWindow *)sharedBackground;
4746
+ (NSMutableArray *)sharedQueue;
4847
+ (SIAlertView *)currentAlertView;
48+
4949
+ (BOOL)isAnimating;
5050
+ (void)setAnimating:(BOOL)animating;
51-
+ (void)removeSharedBackground;
51+
52+
+ (void)showBackground;
53+
+ (void)hideBackgroundAnimated:(BOOL)animated;
5254

5355
@end
5456

5557
#pragma mark - SIBackgroundWindow
5658

5759
@interface SIAlertBackgroundWindow : UIWindow
5860

59-
- (void)show;
60-
- (void)hideAnimated:(BOOL)animated;
6161

6262
@end
6363

6464
@interface SIAlertBackgroundWindow ()
6565

66-
@property (nonatomic, strong) UIView *backgroundView;
66+
@property (nonatomic, assign) SIAlertViewBackgroundStyle style;
6767

6868
@end
6969

7070
@implementation SIAlertBackgroundWindow
7171

72-
- (id)initWithFrame:(CGRect)frame
72+
- (id)initWithFrame:(CGRect)frame andStyle:(SIAlertViewBackgroundStyle)style
7373
{
7474
self = [super initWithFrame:frame];
7575
if (self) {
76-
self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
77-
self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
78-
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
79-
[self addSubview:self.backgroundView];
80-
76+
self.style = style;
8177
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
8278
self.opaque = NO;
8379
self.windowLevel = UIWindowLevelAlert;
8480
}
8581
return self;
8682
}
8783

88-
- (void)show
89-
{
90-
[UIView animateWithDuration:0.3
91-
animations:^{
92-
self.backgroundView.alpha = 1;
93-
}];
94-
}
95-
96-
- (void)hideAnimated:(BOOL)animated
84+
- (void)drawRect:(CGRect)rect
9785
{
98-
if (!animated) {
99-
[SIAlertView removeSharedBackground];
100-
return;
86+
CGContextRef context = UIGraphicsGetCurrentContext();
87+
switch (self.style) {
88+
case SIAlertViewBackgroundStyleGradient:
89+
{
90+
size_t locationsCount = 2;
91+
CGFloat locations[2] = {0.0f, 1.0f};
92+
CGFloat colors[8] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f};
93+
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
94+
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, locationsCount);
95+
CGColorSpaceRelease(colorSpace);
96+
97+
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
98+
CGFloat radius = MIN(self.bounds.size.width, self.bounds.size.height) ;
99+
CGContextDrawRadialGradient (context, gradient, center, 0, center, radius, kCGGradientDrawsAfterEndLocation);
100+
CGGradientRelease(gradient);
101+
break;
102+
}
103+
case SIAlertViewBackgroundStyleSolid:
104+
{
105+
[[UIColor colorWithWhite:0 alpha:0.5] set];
106+
CGContextFillRect(context, self.bounds);
107+
break;
108+
}
101109
}
102-
[UIView animateWithDuration:0.3
103-
animations:^{
104-
self.backgroundView.alpha = 0;
105-
}
106-
completion:^(BOOL finished) {
107-
[SIAlertView removeSharedBackground];
108-
}];
109110
}
110111

111112
@end
@@ -640,15 +641,6 @@ - (id)initWithTitle:(NSString *)title andMessage:(NSString *)message
640641

641642
#pragma mark -
642643

643-
+ (SIAlertBackgroundWindow *)sharedBackground
644-
{
645-
if (!__si_alert_background_window) {
646-
__si_alert_background_window = [[SIAlertBackgroundWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
647-
[__si_alert_background_window makeKeyAndVisible];
648-
}
649-
return __si_alert_background_window;
650-
}
651-
652644
+ (NSMutableArray *)sharedQueue
653645
{
654646
if (!__si_alert_queue) {
@@ -677,10 +669,35 @@ + (void)setAnimating:(BOOL)animating
677669
__si_alert_animating = animating;
678670
}
679671

680-
+ (void)removeSharedBackground
672+
+ (void)showBackground
681673
{
682-
[__si_alert_background_window removeFromSuperview];
683-
__si_alert_background_window = nil;
674+
if (!__si_alert_background_window) {
675+
__si_alert_background_window = [[SIAlertBackgroundWindow alloc] initWithFrame:[UIScreen mainScreen].bounds
676+
andStyle:[SIAlertView currentAlertView].backgroundStyle];
677+
[__si_alert_background_window makeKeyAndVisible];
678+
__si_alert_background_window.alpha = 0;
679+
[UIView animateWithDuration:0.3
680+
animations:^{
681+
__si_alert_background_window.alpha = 1;
682+
}];
683+
}
684+
}
685+
686+
+ (void)hideBackgroundAnimated:(BOOL)animated
687+
{
688+
if (!animated) {
689+
[__si_alert_background_window removeFromSuperview];
690+
__si_alert_background_window = nil;
691+
return;
692+
}
693+
[UIView animateWithDuration:0.3
694+
animations:^{
695+
__si_alert_background_window.alpha = 0;
696+
}
697+
completion:^(BOOL finished) {
698+
[__si_alert_background_window removeFromSuperview];
699+
__si_alert_background_window = nil;
700+
}];
684701
}
685702

686703
#pragma mark - Setters
@@ -732,10 +749,13 @@ - (void)show
732749
self.willShowHandler(self);
733750
}
734751

735-
// transition for background if needed
736-
if ([SIAlertView sharedQueue].count == 1) {
737-
[[SIAlertView sharedBackground] show];
738-
}
752+
self.visible = YES;
753+
754+
[SIAlertView setAnimating:YES];
755+
[SIAlertView setCurrentAlertView:self];
756+
757+
// transition background
758+
[SIAlertView showBackground];
739759

740760
self.viewController = [[SIAlertViewController alloc] initWithNibName:nil bundle:nil];
741761
self.viewController.alertView = self;
@@ -751,11 +771,6 @@ - (void)show
751771
}
752772
[self.alertWindow makeKeyAndVisible];
753773

754-
self.visible = YES;
755-
756-
[SIAlertView setAnimating:YES];
757-
[SIAlertView setCurrentAlertView:self];
758-
759774
[self.viewController transitionInCompletion:^{
760775
if (self.didShowHandler) {
761776
self.didShowHandler(self);
@@ -832,14 +847,16 @@ - (void)dismissAnimated:(BOOL)animated cleanup:(BOOL)cleanup
832847
[self.viewController transitionOutCompletion:dismissComplete];
833848

834849
if ([SIAlertView sharedQueue].count == 1) {
835-
[[SIAlertView sharedBackground] hideAnimated:YES];
850+
// [[SIAlertView sharedBackground] hideAnimated:YES];
851+
[SIAlertView hideBackgroundAnimated:YES];
836852
}
837853

838854
} else {
839855
dismissComplete();
840856

841857
if ([SIAlertView sharedQueue].count == 0) {
842-
[[SIAlertView sharedBackground] hideAnimated:NO];
858+
// [[SIAlertView sharedBackground] hideAnimated:NO];
859+
[SIAlertView hideBackgroundAnimated:YES];
843860
}
844861
}
845862
}

SIAlertViewExample/SIAlertViewExample/ViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ - (IBAction)alert3:(id)sender
130130
NSLog(@"OK Clicked");
131131
}];
132132
alertView.transitionStyle = SIAlertViewTransitionStyleDropDown;
133+
alertView.backgroundStyle = SIAlertViewBackgroundStyleSolid;
133134

134135
alertView.willShowHandler = ^(SIAlertView *alertView) {
135136
NSLog(@"%@, willShowHandler3", alertView);

0 commit comments

Comments
 (0)