1:tableview里面的cell有比较多而且面积比较大的按钮的时候,滑动容易失效。因为按钮的触摸屏蔽了tableview的滑动触摸,所以可以在cell里面的按钮,设置按钮的吞噬触摸失效。而因为吞噬触摸失效带来的问题,自己可以通过一些变量来达到目的。一些代码如下:
propRect:setSwallowTouches(false) --设置吞噬失效 propRect是按钮的名字
propRect:addTouchEventListener(function (sender,eventType)
if eventType == ccui.TouchEventType.ended then --可以在ended、began里操作
if LuckyShopLayer._bDrag or LuckyShopLayer._bDrag2 or LuckyShopLayer._bDrag3 then --因为吞噬失效设置的一些变量(和第二点有联系)
LuckyShopLayer._bDrag2 = false
LuckyShopLayer._bDrag3 =false
return
end
LuaInterFace.PlayerEffect("Sound.bundle/Bt_Press.mp3") --播放音效的接口
LuaInterFace.ShowPropDescribe(propId) --弹出介绍框的接口
end
end)
2:tableview里面,设置按钮之后,滑动到不可见区域的时候,触摸也会出发。这是官方的bug,没有修复。处理这个情况,一般是自己设置一个layer区域,加触摸判断和变量,来达到目的。代码和解析如下:
--注册触摸事件
local function onTouchBegin(touch, event)
local node = event:getCurrentTarget()
local locationInNode = node:convertToNodeSpace(touch:getLocation())
local s = LuckyShopLayer._tableView:getContentSize()
local rect = cc.rect(LuckyShopLayer._tableView:getPositionX() ,LuckyShopLayer._tableView:getPositionY() , 1 ,1)--因为tableview里的触摸是不经过这里判断的,所以当触摸这个点以外的区域,我们都改变变量。这样玩家只要触摸到tableview以外的区域,都会进入判断。(这里要思考一下,比较难懂)
--点击范围判断
if cc.rectContainsPoint(rect,locationInNode) == false then --这里增加一个变量,用来在按钮触发的那里判断,true的时候,按钮end里 就return掉,代码在上面有。
LuckyShopLayer._bDrag3=true
else
LuckyShopLayer._bDrag3=false
end
return true
end
local function onTouchEnded( touch,event )
end
--LuckyShopLayer._uiLayer是tableview的父类。
local listener = cc.EventListenerTouchOneByOne:create()
listener:registerScriptHandler(onTouchBegin, cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
listener:setSwallowTouches(true)
local eventDispatcher = LuckyShopLayer._uiLayer:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, LuckyShopLayer._uiLayer)