切り替えのアニメーション
ボタンを使ってビューからビューへ切り替える方法は分かりました。しかし、切り替えがあまりにすばやいので、視覚的に面白くありません。iPhoneユーザーの高い期待に応えるには、切り替えにアニメーションを追加する必要があります。SDKのAPIを使えばこれを簡単に実現できます。
「VCExampleViewController.m」ファイルに、以下のコードの太字部分を追加します。
-(IBAction) displayView:(id) sender{
secondViewController = [[SecondViewController alloc]
initWithNibName:@"SecondView"
bundle:nil];
[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view cache:YES];
[self.view addSubview:secondViewController.view];
[UIView commitAnimations];
}
このコードは、以下のパラメータを設定して、切り替え処理にアニメーションを追加します。
- Animation duration(アニメーション時間)を1秒に設定
- Animation curve(アニメーションカーブ)をUIViewAnimationCurveEaseInOutに設定(これ以外に使える設定として、UIViewAnimationCurveEaseIn、UIViewAnimationCurveEaseOut、UIViewAnimationCurveLinearがあります)
- Animation transition type(アニメーション遷移タイプ)をUIViewAnimationTransitionCurlDownに設定(これ以外に使える設定として、UIViewAnimationTransitionFlipFromLeft、UIViewAnimationTransitionFlipFromRight、UIViewAnimationTransitionCurlUpがあります)
[Command]-[r]キーを押してアプリケーションをもう一度テストします。[Display SecondView]ボタンを押したときの動作がどう変わったか確認します(図16を参照)。
当然、逆方向に切り替えるときにもアニメーションが必要です。そこでXcodeに戻り、「SecondViewController.m」ファイルに次のコードを追加します。
-(IBAction) btnReturn:(id) sender {
[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp
forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
}
[Command]-[r]キーを押してアプリケーションをテストします。2つ目のビューで[Return]ボタンを押すと、2つ目のビューでもページをめくるアニメーションが実行されます(図17を参照)。
ビュー間のデータの受け渡し
ビューからビューへデータを渡すことが必要になる場合があります。最も簡単な方法は、渡す先のビューにプロパティを作成し、呼び出し側のビューからそのプロパティを設定(または取得)することです。
その一例を示します。「VCExampleViewController.xib」ファイルをダブルクリックし、ViewウィンドウにDatePickerビューを追加します(図18を参照)。
「VCExampleViewController.h」ファイルで、このDatePickerビューのアウトレットを作成し、それをプロパティとして公開します。
#import <UIKit/UIKit.h>
@interface VCExampleViewController : UIViewController {
//---outlet for the DatePicker view---
IBOutlet UIDatePicker *datePicker;
}
//---expose this outlet as a property---
@property (nonatomic, retain) UIDatePicker *datePicker;
-(IBAction) displayView:(id) sender;
@end
VCExampleViewController.xibウィンドウで、[Control]キーを押しながら[File's Owner]項目をDatePickerビューへドラッグし、[datePicker]を選択します。
「SecondViewController.h」ファイルで、オブジェクトタイプUIDatePickerを作成し、プロパティとして公開します。
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
//---object of type UIDatePicker---
UIDatePicker *selectedDatePicker;
}
//---expose the object as a property---
@property (nonatomic, retain) UIDatePicker *selectedDatePicker;
-(IBAction) btnReturn:(id) sender;
@end
「SecondViewController.m」ファイルで、viewDidLoadメソッドに以下のコード(太字部分)を追加します。
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize selectedDatePicker;
- (void)viewDidLoad {
//---display the date and time selected in the previous view---
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMM dd, yyyy HH:mm"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Date and time selected"
message:[formatter stringFromDate:selectedDatePicker.date]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[super viewDidLoad];
}
- (void)dealloc {
//---release the memory used by the property---
[selectedDatePicker release];
[super dealloc];
}
最後に、「VCExampleViewController.m」ファイルで、以下のコードの太字部分を追加します。
#import "VCExampleViewController.h"
#import "SecondViewController.h"
@implementation VCExampleViewController
SecondViewController *secondViewController;
@synthesize datePicker;
-(IBAction) displayView:(id) sender{
secondViewController = [[SecondViewController alloc]
initWithNibName:@"SecondView"
bundle:nil];
//---set the property of the second view with the DatePicker view in the current view---
secondViewController.selectedDatePicker = datePicker;
[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:
UIViewAnimationTransitionCurlDown
forView:self.view cache:YES];
[self.view addSubview:secondViewController.view];
[UIView commitAnimations];
}
[Command]-[r]キーを押してアプリケーションをテストします。DatePickerビューで日付と時刻を選択し、[Display SecondView]ボタンを押します。1つ目のビューで選択した日付と時刻が2つ目のビューに表示されます(図19を参照)。
この記事では、View Controllerの操作と、iPhoneアプリケーションに新しいView Controllerを追加する方法を説明しました。2つのビューを切り替える方法や、切り替えにアニメーションを表示する方法、2つのビューの間で値を受け渡す方法も説明しました。このような操作はiPhoneプログラミングの基本なので、これを理解すれば独自のiPhoneアプリケーションの作成を始めるのに役立つでしょう。




