iOSのアプリを制作する上で、最も重要かもしれない要素であるアニメーション。ブロックを使えばアニメーションのソースコードも分かりやすくまとめることが出来る。
INDEX
ブロックを使用したアニメーション
- 単独処理のサンプル
- [code]
[UIView animateWithDuration:1.0f // 1.0秒間かけて
delay:0.0f // 0.0秒後からスタート
//options:UIViewAnimationOptionRepeat // 繰り返しオプション
options:UIViewAnimationOptionCurveEaseIn| // EaseInカーブ
UIViewAnimationOptionAllowUserInteraction // アニメーション中にユーザによるViewの操作を許可
animations:^{
// アニメーション終了時の状態を記述
TestView.alpha = 1.0;
}
completion:^(BOOL finished) {
// 終了時の処理(メソッドをネストすればメソッドチェーンアニメーションになる)
}];
[/code] - 連続処理のサンプル
- [code]
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn|
UIViewAnimationOptionAllowUserInteraction
animations:^{
TestView.alpha = 1.0;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:1.0f
delay:1.0f
options:UIViewAnimationOptionCurveEaseOut|
UIViewAnimationOptionAllowUserInteraction
animations:^{
TestView.alpha = 0.0;
}
completion:^(BOOL finished) {
[TestView removeFromSuperview];
}];
}];
[/code]
まとめ
連続処理を使ってアニメーションをつないでいけば、ある程度複雑なアニメーションも実現可能になるので便利。