Skip to content

Commit caabf57

Browse files
committed
add SIAlertViewBackgroundStyle support
1 parent a9f98e9 commit caabf57

3 files changed

Lines changed: 46 additions & 15 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: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

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)