How to listen for both tap and gestures at the same time in Adobe AIR for iOS?

10,085

Solution 1

You can use standard mouse events for tap.

This would maintain gesture multitouch input mode.

Otherwise, Gestouch framework: NUI gestures detection framework for mouse, touch and multitouch AS3 development at GitHub might be of interest.

Also note performance impacts of touch / gesture event model:

Both touch and gesture input can be multitouch input depending on the user’s device. ActionScript provides API for handling touch events, gesture events, and individually tracked touch events for multitouch input.

Note: Listening for touch and gesture events can consume a significant amount of processing resources (equivalent to rendering several frames per second), depending on the computing device and operating system. It is often better to use mouse events when you do not actually need the extra functionality provided by touch or gestures. When you do use touch or gesture events, consider reducing the amount of graphical changes that can occur, especially when such events can be dispatched rapidly, as during a pan, rotate, or zoom operation. For example, you could stop animation within a component while the user resizes it using a zoom gesture.

Solution 2

import flash.events.EventDispatcher;
import flash.events.TouchEvent;
import flash.net.Responder;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;

public class SwipeAndTap extends EventDispatcher
{
    private var fingerX:int;
    private var fingerY:int;
    private var elem:Object;

    public function SwipeAndTap(_elem:Object)
    {
        Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

        elem = _elem;
        elem.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
        elem.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
        elem.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
    }
    private function onTouchBegin(e:TouchEvent):void 
    {
        fingerX = e.stageX;
        fingerY = e.stageY;
    }
    private function onTouchMove(e:TouchEvent):void 
    {
        if(e.stageX > (fingerX+150) && (e.stageY > (fingerY-100) && e.stageY < (fingerY+100) ) )
        {
            // swipe right
            dispatchEvent(new TouchSwipeRight(TouchSwipeRight.SWIPE_RIGHT, e));
        }
        else if(e.stageX < (fingerX-150) && (e.stageY > (fingerY-100) && e.stageY < (fingerY+100) ) )
        {
            // swipe left
            dispatchEvent(new TouchSwipeLeft(TouchSwipeLeft.SWIPE_LEFT, e));
        }
    }

    private function onTouchEnd(e:TouchEvent):void 
    { 
        // e.touchPointID;
        if(e.stageX > (fingerX-40) && e.stageX < (fingerX+40))
        {
            dispatchEvent(new TouchEventTap(TouchEventTap.TAP, e));
            elem.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
            elem.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
            elem.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
        }

    }
}

Example:

var SAT:SwipeAndTap = new SwipeAndTap(stage);
    SAT.addEventListener(TouchEventTap.TAP, LangSelected);
    SAT.addEventListener(TouchSwipeRight.SWIPE_RIGHT, ENtoPL);
    SAT.addEventListener(TouchSwipeLeft.SWIPE_LEFT, PLtoEN);

Solution 3

I'm not quite sure if you need to set TOUCH_POINT for basic tapping. It should work just as well if you've got GESTURE set. You can simulate it with mouse events.

At any rate, the default AIR gesture support isn't that good anyways, so it might not work that way, hence I'd recommend looking into the Gestouch library. You get much more sophisticated gesture support that work very well. I've been using it in my Flex/AS3 projects for many month now and I'm quite happy with it.

Share:
10,085
Can Poyrazoğlu
Author by

Can Poyrazoğlu

Has most experience in iOS programming and UI design. Loves astrophotography, board sports, feeding street animals, authoring his humble blog, and flying drones.

Updated on June 17, 2022

Comments

  • Can Poyrazoğlu
    Can Poyrazoğlu almost 2 years

    I am making an iOS game and I need a detection for both simple tapping and gestures (swipe etc) simultaneously. In AIR, I can only see one setting for the Multitouch input mode: TOUCH_POINT (which works for basic tapping) and GESTURE. But I need both at the same time so changing the mode isn't an option. How can I listen to both at the same time?

    Thanks, Can.

  • Can Poyrazoğlu
    Can Poyrazoğlu over 11 years
    wouldn't using Mouse events for touch be a problem? Maybe they will remove the mouse functionality simualtion on tapping in the future, hence the dedicated event for touch
  • AlBirdie
    AlBirdie over 11 years
    Who do you mean with "they"? Flex is an Apache project, and I don't think the community will decide to make the Flex mobile components to work with touch events only.
  • Can Poyrazoğlu
    Can Poyrazoğlu over 11 years
    Ok sorry I thought it was managed by Adobe. I've now implemented with Mouse events, and using standard gestures it worked really well (and no performance impact in my case).
  • zonabi
    zonabi over 8 years
    but wouldn't using standard Mouse Click events for "Tap" touch disable the ability for multiple simultaneous touches?