Flex 4.5 HSlider 和加速计?

发布于 2024-12-10 15:34:02 字数 2720 浏览 0 评论 0原文

我正在尝试在移动设备上使用 Spark HSlider。我可以让拇指根据加速度计很好地移动。但存在两个问题:

1)当您向任一方向倾斜时,拇指会直接飞离屏幕末端。我在下面添加了代码来尝试阻止此操作,但它不起作用。 2)我有一个小米针,它应该跟随拇指的位置,即它指向它。当我拖动拇指时(当更改事件触发时),这种方法工作得很好,但当加速度计使其移动时,这种方法就不行了。我认为(不确定)如果我可以强制触发更改事件,这可能会起作用?

编辑:跟踪语句显示,当加速度计使拇指移动时,h.value 不会改变。我还删除了对 init() 的重复调用。

代码:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView" creationComplete="init()">
<fx:Script>
    <![CDATA[
        import flash.sensors.Accelerometer;

        import mx.events.FlexEvent; 

        public var a:Accelerometer = new Accelerometer();
        private var xSpeed:Number = 0;
        private var newX:Number = 0;

        private function init():void
        {
            this.addEventListener( Event.ADDED_TO_STAGE , addedToStageHandler );
        }

        private function addedToStageHandler(e:Event):void
        {
            h.value = h.minimum;
            needle.rotationZ = h.value + 180;

            if(Accelerometer.isSupported)
            {
                a.addEventListener(AccelerometerEvent.UPDATE, readA);
                stage.autoOrients = false;
            }
        }

        private function readA(e:AccelerometerEvent):void
        {
            xSpeed -= e.accelerationX * 5;
            newX = h.thumb.x + xSpeed;
            if(h.thumb.x < 0 )
            {
                newX = 0;
                xSpeed = 0;
            }
            else if (newX > stage.stageWidth - h.thumb.width) //(newX > stage.stageWidth - RADIUS)
            {
                newX = stage.stageWidth - h.thumb.width;
                xSpeed = 0; 
            }
            else
            {
                newX += xSpeed

            }
            h.thumb.x += xSpeed;
            trace("thumb.x = " + h.thumb.x);
            needle.rotationZ = h.value + 180;
            trace("h.value = " + h.value);
            trace("needle.rotationZ = " + needle.rotationZ);
        }

        protected function h_changeHandler(event:Event):void
        {
            // TODO Auto-generated method stub
            needle.rotationZ = h.value + 180;
            trace(needle.rotationZ);
        }
    ]]>
</fx:Script>
<s:HSlider change="h_changeHandler(event)" minimum="-54" maximum="54" id="h" width="300" x="158" y="74"/>
<s:BorderContainer x="158" y="200" width="300" height="1">
    <s:Rect id="needle" x="150" y="0" width="2" height="100" rotation="180">
        <s:fill>
            <s:SolidColor color="#FF0000"/>
        </s:fill>
    </s:Rect>
</s:BorderContainer>
</s:View>

I'm trying to use a spark HSlider on a mobile device. I can get the thumb to move just fine in response to the accelerometer. But there are two problems:

1) the thumb flies right off the end of the screen when you tilt it in either direction. I've put in code below to try to stop this but it doesn't work.
2) I've got a little meter needle that is supposed to follow the position of the thumb, i.e. it points at it. This works fine when I drag the thumb -- when the change event is firing -- but not when the accelerometer is making it move. I think (not sure) that this might work if I could force the change event to fire?

Edit: The trace statements show that h.value isn't changing when it's the that accelerometer makes the thumb move. Also I removed a duplicate call to init().

Code:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView" creationComplete="init()">
<fx:Script>
    <![CDATA[
        import flash.sensors.Accelerometer;

        import mx.events.FlexEvent; 

        public var a:Accelerometer = new Accelerometer();
        private var xSpeed:Number = 0;
        private var newX:Number = 0;

        private function init():void
        {
            this.addEventListener( Event.ADDED_TO_STAGE , addedToStageHandler );
        }

        private function addedToStageHandler(e:Event):void
        {
            h.value = h.minimum;
            needle.rotationZ = h.value + 180;

            if(Accelerometer.isSupported)
            {
                a.addEventListener(AccelerometerEvent.UPDATE, readA);
                stage.autoOrients = false;
            }
        }

        private function readA(e:AccelerometerEvent):void
        {
            xSpeed -= e.accelerationX * 5;
            newX = h.thumb.x + xSpeed;
            if(h.thumb.x < 0 )
            {
                newX = 0;
                xSpeed = 0;
            }
            else if (newX > stage.stageWidth - h.thumb.width) //(newX > stage.stageWidth - RADIUS)
            {
                newX = stage.stageWidth - h.thumb.width;
                xSpeed = 0; 
            }
            else
            {
                newX += xSpeed

            }
            h.thumb.x += xSpeed;
            trace("thumb.x = " + h.thumb.x);
            needle.rotationZ = h.value + 180;
            trace("h.value = " + h.value);
            trace("needle.rotationZ = " + needle.rotationZ);
        }

        protected function h_changeHandler(event:Event):void
        {
            // TODO Auto-generated method stub
            needle.rotationZ = h.value + 180;
            trace(needle.rotationZ);
        }
    ]]>
</fx:Script>
<s:HSlider change="h_changeHandler(event)" minimum="-54" maximum="54" id="h" width="300" x="158" y="74"/>
<s:BorderContainer x="158" y="200" width="300" height="1">
    <s:Rect id="needle" x="150" y="0" width="2" height="100" rotation="180">
        <s:fill>
            <s:SolidColor color="#FF0000"/>
        </s:fill>
    </s:Rect>
</s:BorderContainer>
</s:View>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-12-17 15:34:02
  1. 看起来您正在直接移动拇指的位置。您应该更改 HSlider.value 属性并让组件相应地定位拇指。

  2. 仅当通过用户交互(如拖动/单击)更改 HSlider 的值时才会触发更改事件。听起来您可能想监听 valueCommit 事件,每当 HSlider 的值发生更改(无论是通过编程方式还是通过用户交互)时,该事件都会触发。

  1. Looks like you are moving the thumb's position directly. You should instead be changing the HSlider.value property and letting the component position the thumb accordingly.

  2. The change event only fires if the value of the HSlider is changed via a user interaction (like a drag/click). Sounds like you might want to listen for the valueCommit event which will fire whenever the value of the HSlider changes whether that is programmatically or via user interaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文