表示オブジェクトのパンとスクロールFlash Player 9 以降、Adobe AIR 1.0 以降 表示させたい領域に対して大きすぎる表示オブジェクトがある場合、scrollRect プロパティを使用して、表示オブジェクトの可視領域を定義できます。また、ユーザー入力に応じて scrollRect プロパティを変更することで、コンテンツの左右のパンや上下のスクロールが可能になります。 scrollRect プロパティは、Rectangle クラスのインスタンスです。Rectangle クラスは、矩形領域を単一のオブジェクトとして定義する場合に必要な値を結合するクラスです。最初に表示オブジェクトの可視領域を定義するには、新しい Rectangle インスタンスを作成し、これを表示オブジェクトの scrollRect プロパティに割り当てます。その後でスクロールまたはパンするには、scrollRect プロパティを個別の Rectangle 変数に読み込み、目的のプロパティを変更します(例えば、パンするには Rectangle インスタンスの x プロパティを、スクロールするには y プロパティを変更します)。次に、この Rectangle インスタンスを scrollRect プロパティに再度割り当て、変更された値を表示オブジェクトに通知します。 例えば、次に示すコードでは、SWF ファイルの境界内には収まらない高さが設定された bigText という名前の TextField オブジェクトに対する可視領域を定義します。up と down という 2 つのボタンがクリックされると、TextField オブジェクトのコンテンツを上下にスクロールさせる関数が呼び出されます。この関数は、scrollRect Rectangle インスタンスの y プロパティを変更することによってスクロールを行います。 import flash.events.MouseEvent;
import flash.geom.Rectangle;
// Define the initial viewable area of the TextField instance:
// left: 0, top: 0, width: TextField's width, height: 350 pixels.
bigText.scrollRect = new Rectangle(0, 0, bigText.width, 350);
// Cache the TextField as a bitmap to improve performance.
bigText.cacheAsBitmap = true;
// called when the "up" button is clicked
function scrollUp(event:MouseEvent):void
{
// Get access to the current scroll rectangle.
var rect:Rectangle = bigText.scrollRect;
// Decrease the y value of the rectangle by 20, effectively
// shifting the rectangle down by 20 pixels.
rect.y -= 20;
// Reassign the rectangle to the TextField to "apply" the change.
bigText.scrollRect = rect;
}
// called when the "down" button is clicked
function scrollDown(event:MouseEvent):void
{
// Get access to the current scroll rectangle.
var rect:Rectangle = bigText.scrollRect;
// Increase the y value of the rectangle by 20, effectively
// shifting the rectangle up by 20 pixels.
rect.y += 20;
// Reassign the rectangle to the TextField to "apply" the change.
bigText.scrollRect = rect;
}
up.addEventListener(MouseEvent.CLICK, scrollUp);
down.addEventListener(MouseEvent.CLICK, scrollDown);
上記の例に示すように、表示オブジェクトの scrollRect プロパティを操作する場合、cacheAsBitmap プロパティを使用して、Flash Player または AIR が表示オブジェクトのコンテンツをビットマップとしてキャッシュするように指定するのが最適な方法です。こうすると、Flash Player および AIR は、表示オブジェクトがスクロールされるたびに、そのコンテンツ全体を再描画する必要がなくなります。代わりに、キャッシュされたビットマップを使用して、必要な部分だけを画面に直接レンダリングできます。詳しくは、表示オブジェクトのキャッシュを参照してください。 |
|