- pushする際に設定したトランジションをpopする際に「自動で」適用する。
- 一度(白など)一色にフェードアウトした後、次の画面がフェードインする。
#import <QuartzCore/QuartzCore.h>
// AnimNavigationControllerで用いるトランジション付き画面のベースクラス
@interface AnimViewBase : UIViewController
{
UIViewController* _nextViewController;
float _duration;
BOOL _navigationBarHidden;
}
-(id) initWithBaseViewController:(UIViewController*)baseViewController nextViewController:(UIViewController*)nextViewController color:(UIColor*)color duration:(float)duration backButtonTitle:(NSString*)backButtonTitle;
// トランジションアニメーションのデザイン
-(CATransition*) createTransition;
@end
@interface AnimViewBase ()
// トランジション開始タイマーイベント
-(void) onScheduleTransition;
@end
@implementation AnimViewBase
-(id) initWithBaseViewController:(UIViewController*)baseViewController nextViewController:(UIViewController*)nextViewController color:(UIColor*)color duration:(float)duration backButtonTitle:(NSString*)backButtonTitle;
{
if (self = [super init]) {
_nextViewController = [nextViewController retain];
_duration = duration;
self.view.backgroundColor = color;
self.title = baseViewController.title;
// ナビゲーションバーのデザイン
if (backButtonTitle) {
UIBarButtonItem* backButton = [[[UIBarButtonItem alloc] init] autorelease];
backButton.title = backButtonTitle;
self.navigationItem.backBarButtonItem = backButton;
}
// トランジションアニメーションのデザイン
[baseViewController.navigationController.view.layer addAnimation:[self createTransition] forKey:nil];
}
return self;
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_navigationBarHidden = self.navigationController.navigationBarHidden;
self.navigationController.navigationBarHidden = YES;
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = _navigationBarHidden;
}
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// トランジション開始スケジュール
[NSTimer scheduledTimerWithTimeInterval:_duration * 0.5 target:self selector:@selector(onScheduleTransition) userInfo:nil repeats:NO];
}
// トランジション開始タイマーイベント
-(void) onScheduleTransition
{
[self.navigationController.view.layer addAnimation:[self createTransition] forKey:nil];
if (_nextViewController) {
// 進む遷移
[self.navigationController pushViewController:_nextViewController animated:NO];
[_nextViewController release];
_nextViewController = nil;
}
else {
// 戻る遷移
[self.navigationController popViewControllerAnimated:NO];
}
}
// トランジションアニメーションのデザイン
-(CATransition*) createTransition
{
return nil;
}
@end
#import "AnimViewBase.h"
@interface FadeInOutViewController : AnimViewBase
{
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// トランジションアニメーションのデザイン
-(CATransition*) createTransition
{
CATransition* transition = [CATransition animation];
transition.duration = _duration * 0.5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
transition.type = kCATransitionFade;
return transition;
}
@end
@interface UINavigationControllerEx : UINavigationController
@end
#import "AnimViewBase.h"
@interface UINavigationControllerEx ()
@end
@implementation UINavigationControllerEx
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
if (self.viewControllers.count > 1) {
UIViewController* prevView = [self.viewControllers objectAtIndex:self.viewControllers.count - 2];
if ([prevView isKindOfClass:[AnimViewBase class]]) {
// トランジション画面クラスの場合、トランジション設定を取得する
[self.view.layer addAnimation:[((AnimViewBase*)prevView) createTransition] forKey:nil];
animated = NO;
}
}
return [super popViewControllerAnimated:animated];
}
@end
UINavigationControllerEx* navigationController = [[[UINavigationControllerEx alloc] initWithRootViewController:view] autorelease];
NextView* NextView = [[[NextView alloc] init] autorelease];
FadeInOutViewController* fadeView = [[[FadeInOutViewController alloc] initWithBaseViewController:self nextViewController:NextView color:[UIColor whiteColor] duration:0.8 backButtonTitle:@"戻る"] autorelease];
[self.navigationController pushViewController:fadeView animated:NO];
コメントする