표시 객체 패닝 및 스크롤

Flash Player 9 이상, Adobe AIR 1.0 이상

표시 객체가 표시하려는 영역에 비해 너무 큰 경우 scrollRect 속성을 사용하여 표시 객체의 표시 가능한 영역을 정의할 수 있습니다. 또한 사용자 입력에 따라 scrollRect 속성을 변경하여 내용을 왼쪽/오른쪽으로 패닝하거나 위/아래로 스크롤할 수 있습니다.

scrollRect 속성은 사각형 영역을 단일 객체로 정의하는 데 필요한 값을 결합하는 클래스인 Rectangle 클래스의 인스턴스입니다. 표시 객체의 표시 가능한 영역을 처음으로 정의하려면 새 Rectangle 인스턴스를 만들어 표시 객체의 scrollRect 속성에 할당합니다. 나중에 스크롤 또는 패닝하려면 scrollRect 속성을 개별 Rectangle 변수로 읽고 원하는 속성을 변경합니다. 예를 들어, Rectangle 인스턴스의 x 속성을 변경하여 패닝하거나 y 속성을 변경하여 스크롤합니다. 그런 다음 Rectangle 인스턴스를 scrollRect 속성에 다시 할당하여 표시 객체에 변경된 값을 알립니다.

예를 들어, 다음 코드에서는 너무 커서 SWF 파일의 경계 안에 들어가지 않는 bigText 라는 TextField 객체에 대한 표시 가능한 영역을 정의합니다. 두 버튼 up down 을 클릭하면 scrollRect Rectangle 인스턴스의 y 속성을 수정하여 TextField 객체의 내용을 위 또는 아래로 스크롤하는 함수가 호출됩니다.

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에서 표시 객체의 전체 내용을 다시 그릴 필요가 없을 뿐만 아니라, 캐시된 비트맵을 사용하여 필요한 부분을 화면에 직접 렌더링할 수 있습니다. 자세한 내용은 표시 객체 캐싱 을 참조하십시오.