@@ -63,21 +63,17 @@ - (void)hideAnimated:(BOOL)animated;
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;
@@ -89,7 +85,7 @@ - (void)show
8985{
9086 [UIView animateWithDuration: 0.3
9187 animations: ^{
92- self.backgroundView . alpha = 1 ;
88+ self.alpha = 1 ;
9389 }];
9490}
9591
@@ -101,13 +97,41 @@ - (void)hideAnimated:(BOOL)animated
10197 }
10298 [UIView animateWithDuration: 0.3
10399 animations: ^{
104- self.backgroundView . alpha = 0 ;
100+ self.alpha = 0 ;
105101 }
106102 completion: ^(BOOL finished) {
107103 [SIAlertView removeSharedBackground ];
108104 }];
109105}
110106
107+ - (void )drawRect : (CGRect)rect
108+ {
109+ CGContextRef context = UIGraphicsGetCurrentContext ();
110+ switch (self.style ) {
111+ case SIAlertViewBackgroundStyleGradient:
112+ {
113+ size_t locationsCount = 2 ;
114+ CGFloat locations[2 ] = {0 .0f , 1 .0f };
115+ CGFloat colors[8 ] = {0 .0f , 0 .0f , 0 .0f , 0 .0f , 0 .0f , 0 .0f , 0 .0f , 0 .75f };
116+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
117+ CGGradientRef gradient = CGGradientCreateWithColorComponents (colorSpace, colors, locations, locationsCount);
118+ CGColorSpaceRelease (colorSpace);
119+
120+ CGPoint center = CGPointMake (self.bounds .size .width / 2 , self.bounds .size .height / 2 );
121+ CGFloat radius = MIN (self.bounds .size .width , self.bounds .size .height ) ;
122+ CGContextDrawRadialGradient (context, gradient, center, 0 , center, radius, kCGGradientDrawsAfterEndLocation );
123+ CGGradientRelease (gradient);
124+ break ;
125+ }
126+ case SIAlertViewBackgroundStyleSolid:
127+ {
128+ [[UIColor colorWithWhite: 0 alpha: 0.5 ] set ];
129+ CGContextFillRect (context, self.bounds );
130+ break ;
131+ }
132+ }
133+ }
134+
111135@end
112136
113137#pragma mark - SIAlertItem
@@ -643,7 +667,7 @@ - (id)initWithTitle:(NSString *)title andMessage:(NSString *)message
643667+ (SIAlertBackgroundWindow *)sharedBackground
644668{
645669 if (!__si_alert_background_window) {
646- __si_alert_background_window = [[SIAlertBackgroundWindow alloc ] initWithFrame: [UIScreen mainScreen ].bounds];
670+ __si_alert_background_window = [[SIAlertBackgroundWindow alloc ] initWithFrame: [UIScreen mainScreen ].bounds andStyle: [SIAlertView currentAlertView ].backgroundStyle ];
647671 [__si_alert_background_window makeKeyAndVisible ];
648672 }
649673 return __si_alert_background_window;
@@ -732,6 +756,11 @@ - (void)show
732756 self.willShowHandler (self);
733757 }
734758
759+ self.visible = YES ;
760+
761+ [SIAlertView setAnimating: YES ];
762+ [SIAlertView setCurrentAlertView: self ];
763+
735764 // transition for background if needed
736765 if ([SIAlertView sharedQueue ].count == 1 ) {
737766 [[SIAlertView sharedBackground ] show ];
@@ -751,11 +780,6 @@ - (void)show
751780 }
752781 [self .alertWindow makeKeyAndVisible ];
753782
754- self.visible = YES ;
755-
756- [SIAlertView setAnimating: YES ];
757- [SIAlertView setCurrentAlertView: self ];
758-
759783 [self .viewController transitionInCompletion: ^{
760784 if (self.didShowHandler ) {
761785 self.didShowHandler (self);
0 commit comments