如何使用事件来找出手指何时离开图像区域?例如,当您解锁iphone,但将手指移动到离滑块太远的地方(在触摸并移动了一点之后-手指仍在触摸屏幕,但不是滑块),它会跳回到开始位置。当我在if语句中使用event.phase=="ended“时,图像不会返回到指定的位置,除非我在手指仍然”停留“在图像上时松开屏幕。基本上,当手指离开图像区域时,我如何将图像返回到某个点?
发布于 2012-07-20 23:59:22
您需要指定触摸事件的像素范围。如果触摸事件超出此像素范围,则重置图片。
因此,在触摸事件中,执行如下操作,其中触摸的范围是x= 320 -> 480和y= 80 -> 280
local function onTouch(event)
local t = event.target
if (t.x < 320 || t.x > 480) || (t.y > 280 || t.y < 80) then
//reset image
end//你也可以试试这个
local function onTouch(event)
local t = event.target
local phase = event.phase
if phase == "moved" then
t.x = event.x - t.x0
t.y = event.y = t.y0
if (t.x < 320 || t.x > 480) || (t.y > 280 || t.y < 80) then
//reset image
end
end
return true
end
objectName:addEventListener("touch", onTouch)发布于 2013-01-15 10:53:24
你需要将图像设置为焦点,这样即使他们的手指离开了对象,触摸事件也会在对象上触发。
这是我用过的示例代码:
local function switchScreenListener(event)
display.getCurrentStage():setFocus( event.target )
if event.phase == "moved" then
local xBoundry = event.target.x + event.target.width/2 -- remember the reference point!
print ("X Boundry: " .. xBoundry .. ", Current X: " .. event.x);
if event.x > xBoundry then
print ("We swiped out.")
display.getCurrentStage():setFocus( nil )
end
elseif event.phase == "ended" then
display.getCurrentStage():setFocus( nil )
print ("Start: (" .. event.xStart .. ", " .. event.yStart .. "), End: (" .. event.x .. ", " .. event.y .. ")");
local options = {
effect = "slideRight",
params = {
isMuted = isMuted
}
}
storyboard.gotoScene( "view_alphabet", options )
end
endhttps://stackoverflow.com/questions/11582879
复制相似问题