如何从手势点捏缩放图片as3

时间:2014-01-06 03:04:22

标签: actionscript-3

我创建了一个图像对象,我能够放大。 但图像只能在图像中心的视野中。 那我怎么能放大手势点的图像?

picture.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);

function onZoom(e:TransformGestureEvent){
    picture.scaleX *= (e.scaleX+e.scaleY)/2;
    picture.scaleY *= (e.scaleX+e.scaleY)/2;
}

3 个答案:

答案 0 :(得分:2)

试试这个......

import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import fl.motion.MatrixTransformer;

Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom);
function onZoom(event:TransformGestureEvent):void
{
    //trace(e.localX);
    var locX:Number=event.localX;
    var locY:Number=event.localY;
    var stX:Number=event.stageX;
    var stY:Number=event.stageY;
    var prevScaleX:Number=gambar.scaleX;
    var prevScaleY:Number=gambar.scaleY;
    var mat:Matrix;
    var externalPoint=new Point(stX,stY);
    var internalPoint=new Point(locX,locY);

    gambar.scaleX *= event.scaleX;
    gambar.scaleY *= event.scaleY;

    if(event.scaleX > 1 && gambar.scaleX > 6){
    gambar.scaleX=prevScaleX;
    gambar.scaleY=prevScaleY;
    }

    if(event.scaleY > 1 && gambar.scaleY > 6){
    gambar.scaleX=prevScaleX;
    gambar.scaleY=prevScaleY;
    }

    if(event.scaleX < 1.1 && gambar.scaleX < 1){
    gambar.scaleX=prevScaleX;
    gambar.scaleY=prevScaleY;
    }

    if(event.scaleY < 1.1 && gambar.scaleY < 1){
    gambar.scaleX=prevScaleX;
    gambar.scaleY=prevScaleY;

    }
    mat=gambar.transform.matrix.clone();
    MatrixTransformer.matchInternalPointWithExternal(mat,internalPoint,externalPoint);
    gambar.transform.matrix=mat;
}

gambar.addEventListener(MouseEvent.MOUSE_DOWN, f_begin);
gambar.addEventListener(MouseEvent.MOUSE_UP, f_end);
function f_begin(e:MouseEvent):void
{
    gambar.startDrag(); 
}
function f_end(e:MouseEvent):void
{
    gambar.stopDrag();
}
你的照片很可爱..:D

答案 1 :(得分:1)

欢迎使用StackOverflow。我通常不会使用触摸事件,因为我开发的是桌面软件,而不是移动软件。但是,我确实有一个想法。我不会冒险使用代码,因为没有任何编写与触摸相关的事件监听器的经验,我可能会遇到问题。但是,我非常有信心该方法可行。

对于AS3中的每个事件,如您所知,有很多关于它的数据。通常存在于鼠标和触摸相关事件上的一条数据是位置 - 屏幕上的对象或触摸发生的对象。

我建议使用此数据来调整屏幕上图片的位置,方法是使用触摸事件的坐标作为视图的新“中心”。

同样,我没有提供代码,因为我之前从未编写过与触摸相关的事件监听器(我总是使用鼠标和键盘事件。)但是,我会冒险尝试这些信息的可能来源如果触摸与鼠标类似,则可以在onZoom函数中的e.xe.y属性中找到。

我还冒昧地说,你可以使用类似于下面的算法来计算你的图片的新位置。我假设图片的注册点是左上角。 [这不是代码]

w=picture width
h=picture height
x=picture x position on stage
y = picture y position on stage
tx=touch event x on stage
ty=touch event y) on stage

x = (w/2) - tx
y = (h/2) - ty

希望这有帮助!

编辑:要访问名为“图片”的图片上的x,y,w和h,只需使用属性picture.xpicture.ypicture.width和{{1作为你的变量。

另一个警告...... EVENT上的x和y可能在舞台上,也可能在物体本身上。您将需要使用跟踪语句和一些常识来解决这个问题,并且可能需要更复杂的数学运算。不过,我不想给你太多,因为这是课程,就像你说的那样。

答案 2 :(得分:0)

看看GesTouch,它是一个很棒的触摸动作库

Gestouch Library

Gestouch Examples

相关问题