[OSX][iOS]アニメーションの種類(画面遷移)
今回は、色々な失敗をしてしまった。Cocoa toouchで慣れてしまった関係でOS Xとの違いも戸惑った。
TOPのビューに、同じサイズの二つのビューを入れ替えるサンプルだ。
self.pentagonImageView = [[MyImageView alloc] initWithFrame:self.frame];
self.starImageView = [[MyImageView alloc] initWithFrame:self.frame];
[self.pentagonImageView setImageScaling:NSScaleToFit];
[self.pentagonImageView setImage:[NSImage imageNamed:@"pentagon.png"]];
[self.starImageView setImageScaling:NSScaleToFit];
[self.starImageView setImage:[NSImage imageNamed:@"star.png"]];
最初に五角形のビューをサブビュートしてい追加する。
[self addSubview:self.pentagonImageView];
その際、忘れていけないのは、アニメーションを追加する上位ビューのレイヤーを有効にすること。
[self setWantsLayer:YES];
画面遷移のアニメーションを追加する。
self.animations = [NSDictionary dictionaryWithObjectsAndKeys:
[self transitionAnimation], @"subviews",
nil];
アニメーションの定義は以下のとおり。
- (CATransition *)transitionAnimation
{
CATransition *animation = [CATransition animation];
animation.type = kCATransitionMoveIn;
animation.subtype = kCATransitionFromTop;
return animation;
}
マウス押下されると画面を差し替える。
if (nil != [self.pentagonImageView superview]) {
[[self animator] replaceSubview:self.pentagonImageView with:self.starImageView];
}
else if (nil != [self.starImageView superview]) {
[[self animator] replaceSubview:self.starImageView with:self.pentagonImageView];
}
Core Animationでいうtransitionとは、サブビューの追加と削除時の表示と非表示の入れ替わりを想定しているようだ。
関連情報
Core Animation for Max OS X and the iPhone
【Cocoa練習帳】
http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)