如何使用科罗纳SDK收听连续触摸事件



这似乎很容易做到,但我不知道如何监控持续触摸。我想触摸显示器或图像,只要用户没有抬起手指,就继续旋转图像。这是我拥有的代码截图:

local rotate = function(event)
if event.phase == "began" then   
  image1.rotation = image1.rotation + 1
end
return true
end
Runtime:addEventListener("touch", rotate)    

我希望旋转发生,直到手指从屏幕上抬起。感谢您的任何建议。

这个怎么样?

local crate = ...
local handle
local function rotate(event)
    if event.phase == "began" and handle == nil then
        function doRotate()
            handle=transition.to(crate, 
                {delta=true, time=1000, rotation=360, onComplete=doRotate})
        end
        doRotate()
    elseif event.phase == "ended" and handle then 
        transition.cancel(handle)
        handle = nil
    end
end
Runtime:addEventListener("touch", rotate) 

这样可以更好地控制旋转速率。 如果出于某种原因开始丢弃帧,则依赖 enterFrame 可能会出现问题。

此外,手柄和非手柄的检查是为了适应多点触控。 还有其他方法(和更好的方法)来处理这个问题,但这是权宜之计(如果您不使用多点触控,则根本不重要。

我最终做到了。如果您有更好的方法,请发布您的答案!

local direction = 0
function scene:move()
 crate.rotation = crate.rotation + direction
end        
Runtime:addEventListener("enterFrame", scene.move)         
local function onButtonEvent( event )
  if event.phase == "press" then
    direction = 1 -- ( -1 to reverse direction )
  elseif event.phase == "moved" then
  elseif event.phase == "release" then
    direction = 0
  end
  return true
end
local button = widget.newButton{
  id = "rotate_button",    
  label = "Rotate",
  font = "HelveticaNeue-Bold",
  fontSize = 16,
  yOffset = -2,
  labelColor = { default={ 65 }, over={ 0 } },
  emboss = true,
  onEvent = onButtonEvent
} 

最新更新