flash - 如何为移动设备(主要是 iOS)制作可滚动的 UI In Touch Flash CS5

标签 flash actionscript-3 touch flash-cs5 gestures

我想知道如何使整个符号(图像)可滚动?我已经找到了如何进行多点触控捏缩放,但我找不到任何代码示例可以让我只用一根手指垂直滚动图像。

所以我想做的就是用一个手指输入使图像上下滚动。有没有人可以给我任何简单的方法或示例代码?

最佳答案

有两种方法可以使图像在 AS3 移动项目中滚动:使用平移手势(两根手指),或使用鼠标向下、移动和向上(一根手指 - 触摸事件在移动设备上注册为鼠标事件).下面的示例同时支持这两种方法。

注意:对于鼠标事件,您可能需要考虑添加一个计时器来区分点击和拖动(如示例所示)。

// Necessary imports for this example - I use Flash Builder, so I'm not sure what CS5 requires.
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.events.TransformGestureEvent;
import flash.geom.Point;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.utils.getTimer;


public class ScrollExample {
    // The image or other display object you want to scroll
    private var t:DisplayObject;
    // Dragging variables
    private var _prevX:Number; // Not required in this case since we're only scrolling vertically
    private var _prevY:Number;
    private var _dragging:Boolean = false;
    private var _lastMouseEvent:int;
    // Minimum touch time to permit drag (in milliseconds) - I use
    private static const MIN_DRAG_TIME:Number = 150;



    public function ScrollExample() {
        // Switch multitouch mode to support gestures (touch/mouse events are still registered)
        Multitouch.inputMode = MultitouchInputMode.GESTURE;

        // For my applications, I have found that the stage is much more responsive to touch events, but you may want to change "stage" here to "t"
        // Pan Gesture - two fingers up and down - like the scroll on a Mac
        stage.addEventListener(TransformGestureEvent.GESTURE_PAN, onPan);
        // Mouse down, move, and up - one finger drag
        stage.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveDrag);
        stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
    }


    private function onPan(e:TransformGestureEvent):void {
        // Move target display object by equivalent offset from pan object
        // For only vertical scrolling, don't use X!
        //t.x += e.offsetX;
        t.y += e.offsetY;
    }


    private function onStartDrag(e:MouseEvent):void {
        // Start timer to differentiate between click and drag
        _lastMouseEvent = getTimer();

        // Start dragging
        _dragging = true;
        // Set drag location values to track how drag is occuring
        // For only vertical scrolling, don't use X!
        //_prevX = e.stageX;
        _prevY = e.stageY;
     }


     private function onMoveDrag(e:MouseEvent):void {
         // If mouse down for less than minimum time, don't drag
         if (getTimer() > _lastMouseEvent + MIN_DRAG_TIME && _dragging) {
             // Move target display object to a valid location - prevents scrolling too far
             // Not using X...
             // t.x = ValidXDragPosition(e);
             t.y = ValidYDragPosition(e);

             // Reset drag position values
             _prevX = e.stageX;
             _prevY = e.stageY;
         }
     }


     private function onStopDrag(e:MouseEvent):void {
         // Stop dragging
         _dragging = false;

         // If mouse down time was less than min time, count as click
         if (getTimer() <= _lastMouseEvent + MIN_DRAG_TIME) {
             onClick(e);
         }
     }


     private function onClick(e:MouseEvent):void {
         // Handle your click event here...
     }


     // This function prevents your target display object from moving too far
     // In this example, it stops dragging when the display object boundary is reached
     // (Only showing Y direction)
     private function ValidYDragPosition(e:MouseEvent):Number {
         // Get the requested drag amount
         var requestedPoint:Number = _prevY - e.stageY;

         if (t.y - requestedPoint > 0) {
             // If drag will move target too far down, stop at top of object
             return 0;
         } else if (t.y - requestedPoint < stage.stageHeight - t.height) {
             // If drag will move target too far up, stop at bottom of object
             return stage.stageHeight - t.height;
         } else {
             // Otherwise, allow drag by requested amount
             return t.y - requestedPoint;
         }
     }
 }

关于flash - 如何为移动设备(主要是 iOS)制作可滚动的 UI In Touch Flash CS5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871435/

相关文章:

apache - 在Ubuntu 17.04中获取错误 “Reference air.sdk.path.linux.3.9 not found ”

flash - 如何使用 BitmapData.fillRect()?

flash - 如何使用 flash actionscript 2 更改文本颜色?

apache-flex - Actionscript/Flex 中的 XML 作为 XMLNode

java - 触摸方向

html - 如何使 HTML 中的 Flash 对象全屏显示?

actionscript-3 - AS3 - 检测您是否在不按下鼠标按钮的情况下单击了对象

actionscript-3 - 矢量图形洪水填充算法?

c# - ListBox 类似 ItemsControl 的触摸滚动

javascript - MapBox dragging.disable 函数阻止页面在移动设备上滚动