#import <UIKit/UIKit.h>
// ローディング画面(シングルトン)
@interface LoadingViewController : UIViewController
+ (LoadingViewController*)sharedController;
+ (void)deleteInstance;
+ (void)dismiss;
- (void)show;
@end
LoadingViewController.m
#import "LoadingViewController.h"
#define SUPERVIEW_SUBVIEWS @"SuperViewSubViews"
@interface LoadingViewController ()
{
BOOL _isAddView;
}
@end
@implementation LoadingViewController
static LoadingViewController* _LoadingViewControllerInstance = nil;
bool _canLoadingViewControllerDelete = NO;
+ (LoadingViewController*)sharedController
{
@synchronized(self) {
if (!_LoadingViewControllerInstance) {
_LoadingViewControllerInstance = [[self alloc] init];
}
}
return _LoadingViewControllerInstance;
}
+ (void)deleteInstance
{
if (_LoadingViewControllerInstance) {
@synchronized(_LoadingViewControllerInstance) {
_canLoadingViewControllerDelete = YES;
[_LoadingViewControllerInstance.view.superview.layer removeObserver:_LoadingViewControllerInstance forKeyPath:@"sublayers"];
[_LoadingViewControllerInstance.view removeFromSuperview];
[_LoadingViewControllerInstance release];
_LoadingViewControllerInstance = nil;
_canLoadingViewControllerDelete = NO;
}
}
}
+ (void)dismiss
{
[UIView animateWithDuration:0.2 animations:^(void) {
_LoadingViewControllerInstance.view.alpha = 0.0;
}completion:^(BOOL finished) {
[LoadingViewController deleteInstance];
}];
}
- (id)init
{
if (self = [super init]) {
_isAddView = NO;
}
return self;
}
- (void)show
{
if (_isAddView) return;
CGSize winSize = [[CCDirector sharedDirector] winSize];
AppController* delegate = (AppController*)[UIApplication sharedApplication].delegate;
UINavigationController* rootViewController = delegate.navController;
[rootViewController.view addSubview:self.view];
self.view.frame = CGRectOffset(self.view.frame, (winSize.width - self.view.frame.size.width) / 2, 0);
_isAddView = YES;
self.view.alpha = 0.0;
[UIView animateWithDuration:0.2 animations:^(void) {
self.view.alpha = 1.0;
}completion:^(BOOL finished) {
}];
// 親レイヤを監視し、サブレイヤ追加によりローディング画面が隠れる場合は最前面に配置し直す
[self.view.superview.layer addObserver:self forKeyPath:@"sublayers" options:0 context:SUPERVIEW_SUBVIEWS];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == SUPERVIEW_SUBVIEWS) {
[self.view.superview bringSubviewToFront:self.view];
}
else {
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
@end
コメントする