CCControlSlider on top of CCScrollView - touch drags BOTH

I have a CCControlSlider on top of a CCScrollView.

Scrollview shouldn’t scroll on moving slider, but when I try to move slider it moves scrollview too.

I have checked that CCControlSlider::ccTouchBegan returns true (which means it should swallow/consume the touch) but it still drags the scrollview under it (both part of same layer).

I have tried to put both slider and the scrollview on different layers, still same behavior. I have multiple sliders so I can’t make the scrollview a child of the slider. (I have a few other buttons etc also on top of CCScrollView, none of them consume the touch and the touches effect the scrollview too).

How do I place a CCControlSlider (and other buttons etc) on top of a CCScrollView so that touching in slider shouldn’t drag/effect the scrollview under it?

I tried playing with setTouchPriority and the order in which CCScrollView and CCControlSlider are added to the layer (and also if they are separate layer, the order in which layers are added to the scene).

Regardless of the orders, the CCScrollView always gets the touch event first. How do I make CCControlSlider get the touch event before CCScrollView? Will that solve my problem?

I found the problem and solution,

Problem:

  1. Changing touch priority of a sprite does not change touch priority of its children.
  2. CCScrollView have a touch priority 0 by default
  3. CCControls have a touch priority 1 by default
  4. CCScrollView does not consume touches, it passes them to other sprites.

Cocos2dx does this because usually controls are placed on top of scrollview like in menu etc and must move with the scrollview. My problem was because I had a scrollview under my controls but didn’t want them to move together.

Solution:

  1. Set CCScrollView touch priority to 2.

  2. Inherit from CCScrollView to implement registerWithTouchDispatcher with bSwallowsTouches=true like this:

     virtual void registerWithTouchDispatcher() {
         CCDirector::sharedDirector()->getTouchDispatcher()->
           addTargetedDelegate(this, CCLayer::getTouchPriority(), true);
     }
    

References:
http://www.cocos2d-x.org/boards/6/topics/28094
http://www.cocos2d-x.org/boards/6/topics/31971

1 Like