@@ -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}
0 commit comments