2013-04-08 15:25:00 +04:00
|
|
|
//
|
|
|
|
// BlockBackground.m
|
|
|
|
// arrived
|
|
|
|
//
|
|
|
|
// Created by Gustavo Ambrozio on 29/11/11.
|
|
|
|
// Copyright (c) 2011 N/A. All rights reserved.
|
|
|
|
//
|
2013-04-08 16:17:47 +04:00
|
|
|
|
2013-04-08 15:25:00 +04:00
|
|
|
#import "BlockBackground.h"
|
|
|
|
|
|
|
|
@implementation BlockBackground
|
|
|
|
|
2013-04-08 16:17:47 +04:00
|
|
|
@synthesize backgroundImage = _backgroundImage;
|
|
|
|
@synthesize vignetteBackground = _vignetteBackground;
|
|
|
|
|
2013-04-08 15:25:00 +04:00
|
|
|
static BlockBackground *_sharedInstance = nil;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
+ (BlockBackground *)sharedInstance
|
2013-04-08 15:25:00 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (_sharedInstance != nil)
|
|
|
|
{
|
|
|
|
return _sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synchronized(self)
|
|
|
|
{
|
|
|
|
if (_sharedInstance == nil)
|
|
|
|
{
|
|
|
|
[[[self alloc] init] autorelease];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedInstance;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
+ (id)allocWithZone:(NSZone *)zone
|
2013-04-08 15:25:00 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
@synchronized(self)
|
|
|
|
{
|
|
|
|
if (_sharedInstance == nil)
|
|
|
|
{
|
|
|
|
_sharedInstance = [super allocWithZone:zone];
|
|
|
|
return _sharedInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NSAssert(NO, @ "[BlockBackground alloc] explicitly called on singleton class.");
|
|
|
|
return nil;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
2013-04-08 15:25:00 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return self;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)retain
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return self;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned)retainCount
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return UINT_MAX;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (oneway void)release
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)autorelease
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return self;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (void)setRotation:(NSNotification *)notification
|
2013-04-08 15:25:00 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
|
|
|
|
|
|
CGRect orientationFrame = [UIScreen mainScreen].bounds;
|
|
|
|
|
|
|
|
if ((UIInterfaceOrientationIsLandscape(orientation) &&
|
|
|
|
orientationFrame.size.height > orientationFrame.size.width) ||
|
|
|
|
(UIInterfaceOrientationIsPortrait(orientation) &&
|
|
|
|
orientationFrame.size.width > orientationFrame.size.height))
|
|
|
|
{
|
|
|
|
float temp = orientationFrame.size.width;
|
|
|
|
orientationFrame.size.width = orientationFrame.size.height;
|
|
|
|
orientationFrame.size.height = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.transform = CGAffineTransformIdentity;
|
|
|
|
self.frame = orientationFrame;
|
|
|
|
|
|
|
|
CGFloat posY = orientationFrame.size.height / 2;
|
|
|
|
CGFloat posX = orientationFrame.size.width / 2;
|
|
|
|
|
|
|
|
CGPoint newCenter;
|
|
|
|
CGFloat rotateAngle;
|
|
|
|
|
|
|
|
switch (orientation)
|
|
|
|
{
|
|
|
|
case UIInterfaceOrientationPortraitUpsideDown:
|
|
|
|
rotateAngle = M_PI;
|
|
|
|
newCenter = CGPointMake(posX, orientationFrame.size.height - posY);
|
|
|
|
break;
|
|
|
|
case UIInterfaceOrientationLandscapeLeft:
|
|
|
|
rotateAngle = -M_PI / 2.0f;
|
|
|
|
newCenter = CGPointMake(posY, posX);
|
|
|
|
break;
|
|
|
|
case UIInterfaceOrientationLandscapeRight:
|
|
|
|
rotateAngle = M_PI / 2.0f;
|
|
|
|
newCenter = CGPointMake(orientationFrame.size.height - posY, posX);
|
|
|
|
break;
|
|
|
|
default: // UIInterfaceOrientationPortrait
|
|
|
|
rotateAngle = 0.0;
|
|
|
|
newCenter = CGPointMake(posX, posY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.transform = CGAffineTransformMakeRotation(rotateAngle);
|
|
|
|
self.center = newCenter;
|
|
|
|
|
|
|
|
[self setNeedsLayout];
|
|
|
|
[self layoutSubviews];
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
self.windowLevel = UIWindowLevelStatusBar;
|
|
|
|
self.hidden = YES;
|
|
|
|
self.userInteractionEnabled = NO;
|
|
|
|
self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5f];
|
|
|
|
self.vignetteBackground = NO;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(setRotation:)
|
|
|
|
name:UIApplicationDidChangeStatusBarOrientationNotification
|
|
|
|
object:nil];
|
|
|
|
[self setRotation:nil];
|
|
|
|
}
|
|
|
|
return self;
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addToMainWindow:(UIView *)view
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
[self setRotation:nil];
|
|
|
|
|
|
|
|
if ([self.subviews containsObject:view])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (self.hidden)
|
|
|
|
{
|
|
|
|
_previousKeyWindow = [[[UIApplication sharedApplication] keyWindow] retain];
|
|
|
|
self.alpha = 0.0f;
|
|
|
|
self.hidden = NO;
|
|
|
|
self.userInteractionEnabled = YES;
|
|
|
|
[self makeKeyWindow];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.subviews.count > 0)
|
|
|
|
{
|
|
|
|
((UIView *)[self.subviews lastObject]).userInteractionEnabled = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_backgroundImage)
|
|
|
|
{
|
|
|
|
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:_backgroundImage];
|
|
|
|
backgroundView.frame = self.bounds;
|
|
|
|
backgroundView.contentMode = UIViewContentModeScaleToFill;
|
|
|
|
[self addSubview:backgroundView];
|
|
|
|
[backgroundView release];
|
|
|
|
[_backgroundImage release];
|
|
|
|
_backgroundImage = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self addSubview:view];
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reduceAlphaIfEmpty
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (self.subviews.count == 1 ||
|
|
|
|
(self.subviews.count == 2 &&
|
|
|
|
[[self.subviews objectAtIndex:0] isKindOfClass:[UIImageView class]]))
|
|
|
|
{
|
|
|
|
self.alpha = 0.0f;
|
|
|
|
self.userInteractionEnabled = NO;
|
|
|
|
}
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeView:(UIView *)view
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
[view removeFromSuperview];
|
|
|
|
|
|
|
|
UIView *topView = [self.subviews lastObject];
|
|
|
|
if ([topView isKindOfClass:[UIImageView class]])
|
|
|
|
{
|
|
|
|
// It's a background. Remove it too
|
|
|
|
[topView removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.subviews.count == 0)
|
|
|
|
{
|
|
|
|
self.hidden = YES;
|
|
|
|
[_previousKeyWindow makeKeyWindow];
|
|
|
|
[_previousKeyWindow release];
|
|
|
|
_previousKeyWindow = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
((UIView *)[self.subviews lastObject]).userInteractionEnabled = YES;
|
|
|
|
}
|
2013-04-08 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (void)drawRect:(CGRect)rect
|
|
|
|
{
|
|
|
|
if (_backgroundImage || !_vignetteBackground)
|
|
|
|
return;
|
|
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
|
|
|
2013-04-08 16:17:47 +04:00
|
|
|
size_t locationsCount = 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
CGFloat locations[2] = { 0.0f, 1.0f };
|
|
|
|
CGFloat colors[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f };
|
2013-04-08 16:17:47 +04:00
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
2019-11-06 17:24:51 +03:00
|
|
|
CGGradientRef gradient =
|
|
|
|
CGGradientCreateWithColorComponents(colorSpace, colors, locations, locationsCount);
|
2013-04-08 16:17:47 +04:00
|
|
|
CGColorSpaceRelease(colorSpace);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
|
|
|
|
float radius = MIN(self.bounds.size.width, self.bounds.size.height);
|
|
|
|
CGContextDrawRadialGradient(context, gradient, center, 0, center, radius,
|
|
|
|
kCGGradientDrawsAfterEndLocation);
|
2013-04-08 16:17:47 +04:00
|
|
|
CGGradientRelease(gradient);
|
|
|
|
}
|
|
|
|
|
2013-04-08 15:25:00 +04:00
|
|
|
@end
|