電子趣味の部屋

電子系のガジェットやアプリ開発等の趣味の話題を書いてます

iPhoneSDKのUIViewアニメーション

UIViewをアニメーションさせる方法

簡単な方法

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:<アニメーションを完了させるまでの秒数(float)>];
<ここにアニメーション後の状態を記述する。>
[UIView commitAnimations];

例1:UIViewから派生させたビュークラス(view1)を1秒かけてフェードアウトさせる場合

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0f];    // 時間の指定
[view1 setAlpha:0.0f];    // アルファチャンネルを0.0fに
[UIView commitAnimations];

例2:320*480のサイズのUIViewから派生させたビュークラス(view1)を1秒かけて画面上部からスクロールインさせる場合

CGContextRef context = UIGraphicsGetCurrentContext();
[view1 setCenter:CGPointMake(160.0f, -240.0f)];    // 表示する中心座標を表示画面外に設定
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0f];    // 時間の指定
[view1 setCenter:CGPointMake(160.0f, 240.0f)];    // 表示する中心座標を表示画面中央に
[UIView commitAnimations];

例3:320*480のサイズのUIViewから派生させたビュークラス(view1)を1秒かけて画面中心に向かって小さくする場合場合

CGContextRef context = UIGraphicsGetCurrentContext();
[view1 setCenter:CGPointMake(160.0f, 240.0f)];
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0f];    // 時間の指定
[view1 setFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];    // 表示サイズを0に
[UIView commitAnimations];

例4:320*480のサイズのUIViewから派生させたビュークラス2個(view1をview2)を作り、1秒かけてview1を画面左スクロールアウト、view2を画面右からスクロールインさせる場合

CGContextRef context = UIGraphicsGetCurrentContext();
[view1 setCenter:CGPointMake(160.0f, 240.0f)];    // view1の表示する中心座標を表示画面中央に設定
[view2 setCenter:CGPointMake(480.0f, 240.0f)];    // view2の表示する中心座標を表示画面外に設定
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0f];    // 時間の指定
[view1 setCenter:CGPointMake(-160.0f, 240.0f)];   // view1の表示する中心座標を表示画面外に設定
[view2 setCenter:CGPointMake(160.0f, 240.0f)];   // view2の表示する中心座標を表示画面中央に設定
[UIView commitAnimations];
さらにアニメーション終了後にView1を親のViewから削除する場合は、setAnimationDidStopSelectorメソッドを使って、アニメーション終了後に実行するメソッドを定義し、そこで削除します。
- (void) ViewAnimation {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view1 setCenter:CGPointMake(160.0f, 240.0f)];
    [view2 setCenter:CGPointMake(480.0f, 240.0f)];
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(ViewAnimationDidStop)];    // 定義例
    [view1 setCenter:CGPointMake(-160.0f, 240.0f)];
    [view2 setCenter:CGPointMake(160.0f, 240.0f)];
    [UIView commitAnimations];
}
- (void) ViewAnimationDidStop {
    [view1 removeFromSuperview];
}

基礎からのiPhone SDK

基礎からのiPhone SDK

Apple iPod touch 第2世代 8GB MB528J/A A1288

Apple iPod touch 第2世代 8GB MB528J/A A1288

Apple MacBook Air 1.6GHz 13.3インチ MB543J/A

Apple MacBook Air 1.6GHz 13.3インチ MB543J/A