ユーザーインターフェイス / ;CSSを使用したアニメーション

CSSアニメーション

CSSアニメーションは、シンプルで使いやすい標準のCSS3アニメーションAPI に基づいています。 JavaScriptを知らなくても、ほとんどすべてのネイティブビューをアニメーション化できます。 タッチやアクティブ化などの状態の変化が発生するたびに、要素の外観と動作を変更する可能性があります。 複数のフレームを使用して、アニメーションの方向を変更できます。 最後に、CSSアニメーションを使用すると、アニメーションコードをアプリケーションロジックから分離できます。

CSSアニメーションは、CSSアニメーションを説明するスタイルと、アニメーションのスタイルの開始状態と終了状態を示す一連のキーフレーム、および可能な中間ウェイポイントで構成されます。 アニメーション化可能なCSSプロパティを必要なだけ何度でも変更できます。

例1は、「example」アニメーションをボタン要素にバインドします。 アニメーションは4秒間続きます。ボタン要素の背景色を「赤」から「緑」に徐々に変更します。

例1:CSSを使用して簡単なアニメーションを作成する方法

@keyframes example {
	from { background-color: red; }
	to { background-color: green; }
}

.view {
	animation-name: example;
	animation-duration: 4s;
	animation-fill-mode: forwards;
}

アニメーションを機能させるには、アニメーションを要素にバインドする必要があります。

view1.className = "example";
view1.className = "example";
<Button id="myButton" text="{N}" class="example"/>
animation-durationプロパティが指定されていない場合、アニメーションはデフォルト値の0.3秒を使用します。

アニメート可能なプロパティ

CSSアニメーションは、コードベースのアニメーションで使用されるのと同じアニメーション可能なプロパティをサポートしています。

単一の x または y フィールドを scale および translate に設定することはできません。 translate で x のみを設定すると、 y は 0 と見なされます。 スケールで y のみを設定した場合、 x は 1 と見なされます。

アニメーションのプロパティ

CSSアニメーションは、animationプロパティとそのサブプロパティを使用して定義されます。 それらには、タイミング、持続時間、遅延、その他のアニメーションプロパティが含まれます。 実際のアニメーションの外観は、@keyframesルールで定義されます。

次のリストは、すべてのアニメーションプロパティを示しています。

アニメーションキーフレーム

要素が遷移する複数のポイントを設定するには、例2に示す@keyframesルールを使用します。 これには、アニメーション名、アニメーションブレークポイント、およびアニメーション化するためのプロパティが含まれます。

例2:@keyframesルールの使用方法

@keyframes example {
	from { background-color: red; }
	to { background-color: green; }
}

例2では、2つのキーフレームを持つアニメーションを定義しています。 「from」は0%(アニメーションの開始)を表し、「to」は100%(最終値)を表します。 パーセント表記を使用して、キーフレームをさらに追加できます。

例3は、アニメーションが25%完了、50%完了、100%完了したときに背景色を変更する方法を示しています。

例3:さまざまなアニメーション段階で背景色を変更する

@keyframes example {
	0%   { background-color: red; }
	25%  { background-color: yellow; }
	50%  { background-color: blue; }
	100% { background-color: green; }
}

例4に示すように、キーフレームに複数のプロパティを設定できます。

例4:異なるアニメーションステージで複数のプロパティを変更する

@keyframes example {
	0%   { background-color: red; transform: translate(0, 0); }
	25%  { background-color: yellow; transform: translate(200, 0); }
	50%  { background-color: blue; transform: translate(200, 200);  }
	75%  { background-color: green; transform: translate(0, 200); }
	100% { background-color: red; transform: translate(0, 0); }
}

例5に示すように、キーフレームを組み合わせることができます。

例5:複数のキーフレームのプロパティを設定する

@keyframes example {
	0%, 50% { background-color: red; transform: translate(0, 0); }
	25%, 75% { background-color: yellow; transform: translate(200, 0); }
	100% { background-color: red; transform: translate(0, 0); }
}

アニメーションを遅らせる

animation-delayプロパティは、アニメーションの開始前に遅延時間(秒単位)を指定します。

例6:アニメーションが開始する前に遅延を設定する

.view {
	background-color: red;
	animation-name: example;
	animation-duration: 4s;
	animation-delay: 2s;
}

アニメーション実行回数の設定

animation-iteration-countのプロパティは、アニメーションを実行する回数を定義します。 例7のアニメーションは、停止する前に2回再生されます。

例7:animation-iteration-countプロパティの使用方法

.view {
	background-color: red;
	animation-name: example;
	animation-duration: 4s;
	animation-iteration-count: 2;
}

アニメーションを永久に再生する場合は、このプロパティをinfiniteに設定します。

animation-iteration-count: infinite;

アニメーションの速度曲線を指定する

animation-timing-functionプロパティは、アニメーションの速度曲線を指定します。次の値のいずれかを指定できます。

例8:3次ベジェ関数を使用して速度曲線を指定する方法

.view {
	animation-name: example;
	animation-timing-function: cubic-bezier(0.1, 0.1, 1.0, 1.0);
}

アニメーションが終了したときの結果を決定する

animation-fill-modeプロパティは、アニメーションが終了したときの要素のスタイルを決定します。 デフォルト値はnoneです。 この場合、アニメーション化された値はすべて、アニメーションが開始される前の状態にリセットされます。 アニメーション中に設定されたプロパティ値を保持するには、forwardsを選択する必要があります。

例9:animation-fill-modeプロパティの使用方法

.view {
	background-color: red;
	animation-name: example;
	animation-duration: 2s;
	animation-fill-mode: forwards;
}

アニメーションの方向

例10に示すように、animation-directionプロパティを使用して、CSSアニメーションを逆方向に再生できます。

例10:アニメーションの方向を逆にする方法

.view {
	background-color: red;
	animation-name: example;
	animation-duration: 4s;
	animation-direction: reverse;
}

アニメーションプロパティの簡略表記

animationプロパティでは、単一の行を持つすべての 7 つのアニメーションのプロパティを設定することができます:

例11:アニメーションの速記プロパティの使用方法

.view {
	animation: example 4s ease-in-out 2s infinite reverse forwards;
}

サポートされている構文は次のとおりです。

animation: name duration timing-function delay iteration-count direction fill-mode;

コンマを使用して、animationプロパティで2つのアニメーションを組み合わせることができます。

例12:アニメーションプロパティで複数のアニメーションを組み合わせる方法

.view {
	animation: example 4s ease-in-out 2s infinite reverse, second-animation-example 5s ease-out;
}

疑似セレクター

擬似セレクターは、要素の特別な状態を定義するために使用されます。たとえば、ユーザーがボタンに触れたとき。擬似セレクターを使用して、アニメーションをトリガーできます。

例13:要素の特別な状態でアニメーションをトリガーする方法

.button {
	background-color: green;
}

.button:highlighted {
	animation-name: highlight;
	animation-duration: 2s;
	animation-fill-mode: forwards;
}

@keyframes highlight {
	from { background-color: yellow; }
	to { background-color: red; }
}
バージョン2.0の時点では、ユーザーがタッチしたことを示すための組み込みの特別な状態highlightedButtonコンポーネントのみにあります。

アニメーション-幅と高さ

例14:ビューの幅と高さをアニメーション化する方法

<GridLayout rows="* *" columns="*" class="home-panel">
	<StackLayout row="0">
		<Label text="{N}" class="big-label first-view" textWrap="true" />
	</StackLayout>
	<StackLayout row="1">
		<Label text="{N}" class="big-label second-view" textWrap="true" />
	</StackLayout>
</GridLayout>
.first-view {
	animation-name: example-width;
	animation-duration: 5s;
	animation-fill-mode: forwards;
}

.second-view {
	animation-name: example-height;
	animation-duration: 5s;
	animation-fill-mode: forwards;
}

@keyframes example-width {
	from { width:140; }
	to { width:200; }
}

@keyframes example-height {
	from { height:140; }
	to { height:200; }
}

デモ

コードからCSSアニメーションにアクセスする

CSSアニメーションをトリガーする最も簡単な方法は、要素のclassNameプロパティを変更することです。

例15:CSSアニメーションをトリガーする方法

var view = page.getViewById("view");
view.className = "transparent";
let view = page.getViewById<viewModule.View>("view");
view.className = "transparent";

CSSで定義されているすべてのキーフレームには、getKeyframeAnimationWithNameメソッドを使用してコードでアクセスできます。 これにより、アニメーションプロパティをさらにカスタマイズできます。

例16:getKeyframeAnimationWithNameメソッドを介してコード内のCSS定義キーフレームにアクセスする

var keyframeAnimation = require("tns-core-modules/ui/animation/keyframe-animation");

var view = page.getViewById("view");
var animationInfo = page.getKeyframeAnimationWithName("bounce");
animationInfo.duration = 2000;
var animation = keyframeAnimation.KeyframeAnimation.keyframeAnimationFromInfo(animationInfo);
animation.play(view).then(() => {
	console.log("Played with code!");
});
import {KeyframeAnimation} from "tns-core-modules/ui/animation/keyframe-animation";

let view = page.getViewById<viewModule.View>("view");
let animationInfo = page.getKeyframeAnimationWithName("bounce");
animationInfo.duration = 2000;
let animation = KeyframeAnimation.keyframeAnimationFromInfo(animationInfo);
animation.play(view).then(() => {
	console.log("Played with code!");
});
入門

コアコンセプト

ユーザーインターフェース

ツール

ハードウェアアクセス

プラグインの開発

リリース

アプリテンプレート

パフォーマンスの最適化

フレームワークモジュール

ガイド

サポートを受ける

トラブルシューティング

Siedkick