0

我将从一个例子开始

ImageButton {
    defaultImageSource: "asset:///images/test_p.png"
    pressedImageSource: "asset:///images/test_p_pressed.png"

    attachedObjects: [ 
        OrientationHandler { //gives "orientation"
            onOrientationAboutToChange: {
                if (orientation == UIOrientation.Landscape) {
                    defaultImageSource = "asset:///images/test_l.png"
                    pressedImageSource = "asset:///images/test_l_pressed.png"
                } else {
                    defaultImageSource = "asset:///images/test_p.png"
                    pressedImageSource = "asset:///images/test_p_pressed.png"
                } 
            }
        }
    ]  
}

屏幕旋转效果很好,当它是横向时它使用 _l,当它是纵向时它使用 _p 图像。问题是,当我在横向启动应用程序时,它会显示 _p,而不是 _l 图像(因为它是默认的)。如何在 onCreationCompleted 中检查方向?

4

1 回答 1

0

我想通了,这真的很明显。您将 id 放入 OrientationHandler,然后在 onCreationCompleted 中使用它。

ImageButton {
    defaultImageSource: "asset:///images/test_p.png" //do I still need this?
    pressedImageSource: "asset:///images/test_p_pressed.png"

    attachedObjects: [ 
        OrientationHandler { //gives "orientation"
        id: orientationHandler //make id so you can call it
            onOrientationAboutToChange: {
                changeImages(orientation);
            }
        }
    ]  
    function changeImages(orientation){
        if (orientation == UIOrientation.Landscape) {
            defaultImageSource = "asset:///images/test_l.png"
            pressedImageSource = "asset:///images/test_l_pressed.png"
        } else {
            defaultImageSource = "asset:///images/test_p.png"
            pressedImageSource = "asset:///images/test_p_pressed.png"
        } 
    }
    onCreationCompleted: {
        changeImages(orientationHandler.orientation); //call it here
    }
}
于 2013-08-29T17:32:45.530 回答