1. 功能介紹

父scroll里包含多個子scroll,父scroll滑動為上下,子scroll滑動為左右。
為了防止在上下滑動父scroll時,出現(xiàn)子scroll左右滑動的情況,寫了以下的解決方法
2.解決方法
父子scroll都新增scroll監(jiān)聽函數(shù)
2.1 子scroll方向檢測
在子scroll滑動的時候,子scroll左右滑動的距離超過我們定義的最小水平距離(self.judgeHMove),且同時上下滑動的距離小于我們定義的最小垂直距離(self.judgeVMove),那么就認(rèn)定子scroll在做橫向偏移,反之縱向
if math.abs(touchDisX) >= self.judgeHMove and self.isMoveV == false
and math.abs(touchDisY) < self.judgeVMove then
self.isMoveH = true
end
2.2 子scroll判斷吞噬
如果判斷為橫向移動,觸摸吞噬,不傳遞給父scroll,這樣子scroll橫向滑動的時候父scroll就不會發(fā)生略微的縱向滑動
如果判斷為縱向移動,子scroll設(shè)置為不可觸摸狀態(tài),這樣父scroll縱向移動時,子scroll不會發(fā)生滑動
if self.isMoveH == true then
self.scroll:setSwallowTouches(true)
elseif self.isMoveV == true then
self.scroll:setSwallowTouches(false)
self.scroll:setTouchEnabled(false)
end
2.3 父類scroll觸摸檢測
當(dāng)父類移動結(jié)束后,我們需要去調(diào)用子類的函數(shù)將剛才設(shè)置的子scroll的不可觸摸轉(zhuǎn)為可觸摸
if type == ccui.TouchEventType.ended then
for i,v in ipairs(self.giftItemOcxs) do
if v.onParentScrollEvent then
v:onParentScrollEvent(target, type)
end
end
elseif type == ccui.TouchEventType.canceled then
for i,v in ipairs(self.giftItemOcxs) do
if v.onParentScrollEvent then
v:onParentScrollEvent(target, type)
end
end
end
2.3 子類的回調(diào)函數(shù)
if event == ccui.TouchEventType.ended then
if self.isMoveV then
self.scroll:setTouchEnabled(true)
self.scroll:setSwallowTouches(false)
self.isMoveV = false
self.isMoveH = false
end
elseif event == ccui.TouchEventType.canceled then
if self.isMoveV then
self.scroll:setTouchEnabled(true)
self.scroll:setSwallowTouches(false)
self.isMoveV = false
self.isMoveH = false
end
end