Cocos2dx

两个scroll重叠,且滑动方向相反

2018-10-24  本文已影响0人  凉拌姨妈好吃

1. 功能介绍

父scroll里包含多个子scroll,父scroll滑动为上下,子scroll滑动为左右。
为了防止在上下滑动父scroll时,出现子scroll左右滑动的情况,写了以下的解决方法

2.解决方法

父子scroll都新增scroll监听函数

2.1 子scroll方向检测

在子scroll滑动的时候,子scroll左右滑动的距离超过我们定义的最小水平距离(self.judgeHMove),且同时上下滑动的距离小于我们定义的最小垂直距离(self.judgeVMove),那么就认定子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就不会发生略微的纵向滑动
如果判断为纵向移动,子scroll设置为不可触摸状态,这样父scroll纵向移动时,子scroll不会发生滑动

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触摸检测

当父类移动结束后,我们需要去调用子类的函数将刚才设置的子scroll的不可触摸转为可触摸

    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 子类的回调函数
    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
上一篇 下一篇

猜你喜欢

热点阅读