9 #import "BlockBackground.h"
13 @synthesize backgroundImage = _backgroundImage;
14 @synthesize vignetteBackground = _vignetteBackground;
20 if (_sharedInstance != nil)
22 return _sharedInstance;
27 if (_sharedInstance == nil)
29 [[[
self alloc] init] autorelease];
33 return _sharedInstance;
36 + (id)allocWithZone:(NSZone *)zone
40 if (_sharedInstance == nil)
42 _sharedInstance = [
super allocWithZone:zone];
43 return _sharedInstance;
46 NSAssert(NO, @
"[BlockBackground alloc] explicitly called on singleton class.");
50 - (id)copyWithZone:(NSZone *)zone
60 - (unsigned)retainCount
65 - (oneway void)release
74 - (void)setRotation:(NSNotification *)notification
76 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
78 CGRect orientationFrame = [UIScreen mainScreen].bounds;
80 if ((UIInterfaceOrientationIsLandscape(orientation) &&
81 orientationFrame.size.height > orientationFrame.size.width) ||
82 (UIInterfaceOrientationIsPortrait(orientation) &&
83 orientationFrame.size.width > orientationFrame.size.height))
85 float temp = orientationFrame.size.width;
86 orientationFrame.size.width = orientationFrame.size.height;
87 orientationFrame.size.height = temp;
90 self.transform = CGAffineTransformIdentity;
91 self.frame = orientationFrame;
93 CGFloat posY = orientationFrame.size.height / 2;
94 CGFloat posX = orientationFrame.size.width / 2;
101 case UIInterfaceOrientationPortraitUpsideDown:
103 newCenter = CGPointMake(posX, orientationFrame.size.height - posY);
105 case UIInterfaceOrientationLandscapeLeft:
106 rotateAngle = -M_PI / 2.0f;
107 newCenter = CGPointMake(posY, posX);
109 case UIInterfaceOrientationLandscapeRight:
110 rotateAngle = M_PI / 2.0f;
111 newCenter = CGPointMake(orientationFrame.size.height - posY, posX);
115 newCenter = CGPointMake(posX, posY);
119 self.transform = CGAffineTransformMakeRotation(rotateAngle);
120 self.center = newCenter;
122 [
self setNeedsLayout];
123 [
self layoutSubviews];
128 self = [
super initWithFrame:[[UIScreen mainScreen] bounds]];
131 self.windowLevel = UIWindowLevelStatusBar;
133 self.userInteractionEnabled = NO;
134 self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5f];
135 self.vignetteBackground = NO;
137 [[NSNotificationCenter defaultCenter]
139 selector:@selector(setRotation:)
140 name:UIApplicationDidChangeStatusBarOrientationNotification
142 [
self setRotation:nil];
147 - (void)addToMainWindow:(UIView *)view
149 [
self setRotation:nil];
151 if ([
self.subviews containsObject:view])
156 _previousKeyWindow = [[[UIApplication sharedApplication] keyWindow] retain];
159 self.userInteractionEnabled = YES;
160 [
self makeKeyWindow];
163 if (
self.subviews.count > 0)
165 ((UIView *)[
self.subviews lastObject]).userInteractionEnabled = NO;
168 if (_backgroundImage)
170 UIImageView *backgroundView = [[UIImageView alloc] initWithImage:_backgroundImage];
171 backgroundView.frame =
self.bounds;
172 backgroundView.contentMode = UIViewContentModeScaleToFill;
173 [
self addSubview:backgroundView];
174 [backgroundView release];
175 [_backgroundImage release];
176 _backgroundImage = nil;
179 [
self addSubview:view];
182 - (void)reduceAlphaIfEmpty
184 if (
self.subviews.count == 1 ||
185 (
self.subviews.count == 2 &&
186 [[
self.subviews objectAtIndex:0] isKindOfClass:[UIImageView
class]]))
189 self.userInteractionEnabled = NO;
193 - (void)removeView:(UIView *)view
195 [view removeFromSuperview];
197 UIView *topView = [
self.subviews lastObject];
198 if ([topView isKindOfClass:[UIImageView
class]])
201 [topView removeFromSuperview];
204 if (
self.subviews.count == 0)
207 [_previousKeyWindow makeKeyWindow];
208 [_previousKeyWindow release];
209 _previousKeyWindow = nil;
213 ((UIView *)[
self.subviews lastObject]).userInteractionEnabled = YES;
217 - (void)drawRect:(CGRect)rect
219 if (_backgroundImage || !_vignetteBackground)
221 CGContextRef context = UIGraphicsGetCurrentContext();
223 size_t locationsCount = 2;
224 CGFloat locations[2] = { 0.0f, 1.0f };
225 CGFloat colors[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f };
226 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
227 CGGradientRef gradient =
228 CGGradientCreateWithColorComponents(colorSpace, colors, locations, locationsCount);
229 CGColorSpaceRelease(colorSpace);
231 CGPoint center = CGPointMake(
self.bounds.size.width / 2,
self.bounds.size.height / 2);
232 float radius = MIN(
self.bounds.size.width,
self.bounds.size.height);
233 CGContextDrawRadialGradient(context, gradient, center, 0, center, radius,
234 kCGGradientDrawsAfterEndLocation);
235 CGGradientRelease(gradient);