给你一段lua 代码,关于多点触控的处理的。
–记录当前触摸的数量
local localsize = 0
–多点触控 是否不能点击状态(当容器移动,或缩放,该值为置为true)
TOUCHES_MOVE_FLAG = nil
–防重叠标志
TOUCHES_OVERLAP_FLAG = nil
–触摸开始点,移动中的点,两点距离,x轴的差值,y轴差值
local localBeganPoint,localMovePoint,distance,dx,dy
local function onTouchesBegan(touches,event)
local size = #touches
–记录当前触摸点的数量
localsize = localsize + size
–如果已经有多点触摸 distance 为真,则return
if distance then return end
if size > 1 then
local p1 = touches:getLocation()
local p2 = touches:getLocation()
distance = (p1.x - p2.x)(p1.x - p2.x) + (p1.y - p2.y)(p1.y - p2.y)
–记录偏移
dx = (p1.x + p2.x)/2 - self:getPositionX()
dy = (p1.y + p2.y)/2 - self:getPositionY()
localBeganPoint = nil
TOUCHES_MOVE_FLAG = true
else
–andriod平台多点触摸,触摸点是依次传出
if not localBeganPoint then
localBeganPoint = touches:getLocation()
else
local p2 = touches:getLocation()
distance = (localBeganPoint.x - p2.x)(localBeganPoint.x - p2.x) + (localBeganPoint.y - p2.y)(localBeganPoint.y - p2.y)
–记录偏移
dx = (localBeganPoint.x + p2.x)/2 - self:getPositionX()
dy = (localBeganPoint.y + p2.y)/2 - self:getPositionY()
localBeganPoint = nil
TOUCHES_MOVE_FLAG = true
end
end
end
–触摸移动监听
local function onTouchesMoved(touches,event)
if not self.touchenabled_ then return end
local size = #touches
if size > 1 then
–缩放
if not distance then return end
local touche_1,touche_2
for i = 1,#touches do
if touches*:getId() == 0 then
touche_1 = touches*
elseif touches*:getId() == 1 then
touche_2 = touches*
end
end
if not touche_1 or not touche_2 then return end
local p1 = touche_1:getLocation()
local p2 = touche_2:getLocation()
local tempdistance = (p1.x - p2.x)(p1.x - p2.x) + (p1.y - p2.y)(p1.y - p2.y)
local temp = math.sqrt(math.abs(tempdistance - distance))
–当移动距离大于3像素,则开始执行一次缩放
if temp > 3 then
if tempdistance > distance then
self:excuteScale(self.currentScale_ + self.scaleSteep_)
else
self:excuteScale(self.currentScale_ - self.scaleSteep_)
end
–边界内, 保持偏移
self:excuteMove(cc.p((p1.x+p2.x)/2 - dx,(p1.y+p2.y)/2 - dy))
distance = tempdistance
end
else
–移动
if not localBeganPoint or distance then return end
local movep = touches:getLocation()
if not localMovePoint then
--设置移动阈值 为 8,即移动 >=8,不应该触发子控件的点击事件
if math.abs(movep.x - localBeganPoint.x) >= 8 or math.abs(localBeganPoint.y - movep.y) >= 8 then
TOUCHES_MOVE_FLAG = true
localMovePoint = movep
end
elseif localMovePoint then
local x,y = self:getPosition()
self:excuteMove(cc.p(x + (movep.x - localMovePoint.x),y + (movep.y - localMovePoint.y)))
localMovePoint = movep
end
end
end
–触摸结束监听
local function onTouchesEnded(touches,event)
localsize = localsize - #touches
if localsize > 0 then return end
localsize = 0
TOUCHES_MOVE_FLAG = nil
TOUCHES_OVERLAP_FLAG = nil
localBeganPoint = nil
localMovePoint = nil
distance = nil
end