iOS 编程的 AS3 缩放 in/out 限制
Zoom in/out limits on AS3 for iOS programming
我的代码有问题。我使用 AS3
进行了基本缩放,使用 two fingers
进行缩放。但是我遇到了麻烦;
例如我需要在 2
中停止放大(正常大小为 1
),然后,我需要最大缩小到 1
。这是我的代码,但如果我快速缩放,缩放会超过 2
。
我需要将缩放限制在 1
和 2
之间。
Multitouch.inputMode = MultitouchInputMode.GESTURE;
escenario.addEventListener(TransformGestureEvent.GESTURE_PAN, fl_PanHandler);
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, fl_ZoomHandler);
function fl_PanHandler(event:TransformGestureEvent):void
{
event.currentTarget.x += event.offsetX;
event.currentTarget.y += event.offsetY;
}
function fl_ZoomHandler(event:TransformGestureEvent):void
{
if (event.scaleX && event.scaleY >= 1 && escenario.scaleX && escenario.scaleY <= 2)
{
escenario.scaleX *= event.scaleX;
escenario.scaleY *= event.scaleY;
trace(escenario.scaleX);
}
}
由于您正在执行 times/equals (*=),您的值很容易超过 if 语句中的阈值 2,因为您在 if 语句之后乘以该值。你可以这样做:
function fl_ZoomHandler(event:TransformGestureEvent):void {
var scale:Number = escenario.scaleX * event.scaleX; //the proposed new scale amount
//you set both the scaleX and scaleY in one like below:
escenario.scaleY = escenario.scaleX = Math.min(Math.max(1,scale), 2);
//^^^^ inside the line above,
//Math.max(1, scale) will return whatever is bigger, 1 or the proposed new scale.
//Then Math.min(..., 2) will then take whatever is smaller, 2 or the result of the previous Math.max
trace(escenario.scaleX);
}
我的代码有问题。我使用 AS3
进行了基本缩放,使用 two fingers
进行缩放。但是我遇到了麻烦;
例如我需要在 2
中停止放大(正常大小为 1
),然后,我需要最大缩小到 1
。这是我的代码,但如果我快速缩放,缩放会超过 2
。
我需要将缩放限制在 1
和 2
之间。
Multitouch.inputMode = MultitouchInputMode.GESTURE;
escenario.addEventListener(TransformGestureEvent.GESTURE_PAN, fl_PanHandler);
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, fl_ZoomHandler);
function fl_PanHandler(event:TransformGestureEvent):void
{
event.currentTarget.x += event.offsetX;
event.currentTarget.y += event.offsetY;
}
function fl_ZoomHandler(event:TransformGestureEvent):void
{
if (event.scaleX && event.scaleY >= 1 && escenario.scaleX && escenario.scaleY <= 2)
{
escenario.scaleX *= event.scaleX;
escenario.scaleY *= event.scaleY;
trace(escenario.scaleX);
}
}
由于您正在执行 times/equals (*=),您的值很容易超过 if 语句中的阈值 2,因为您在 if 语句之后乘以该值。你可以这样做:
function fl_ZoomHandler(event:TransformGestureEvent):void {
var scale:Number = escenario.scaleX * event.scaleX; //the proposed new scale amount
//you set both the scaleX and scaleY in one like below:
escenario.scaleY = escenario.scaleX = Math.min(Math.max(1,scale), 2);
//^^^^ inside the line above,
//Math.max(1, scale) will return whatever is bigger, 1 or the proposed new scale.
//Then Math.min(..., 2) will then take whatever is smaller, 2 or the result of the previous Math.max
trace(escenario.scaleX);
}