CCScrollView与CCControlButton。如何控制触控区域?

时间:2013-05-20 15:34:37

标签: cocos2d-x ccscrolllayer

我的CCScrollView带有CCControlButtons的容器,当按钮滚出可见的CCScrollView区域时,它们也可以被触摸。我该如何控制该区域?

2 个答案:

答案 0 :(得分:3)

受Tomasz的回答启发,我创建了一个替代解决方案,也继承自CCControlButton

bool ScrollableButton::isTouchInside(CCTouch *touch) {
  return !dragging && CCControlButton::isTouchInside(touch);
}

bool ScrollableButton::ccTouchBegan(CCTouch *touch, CCEvent *event) {
  dragging = false;
  return CCControlButton::ccTouchBegan(touch, event);
}

void ScrollableButton::ccTouchMoved(CCTouch *touch, CCEvent *event) {
  if (!dragging && ccpDistance(touch->getLocation(), touch->getStartLocation()) > 25) {
    dragging = true;
  }
  CCControlButton::ccTouchMoved(touch, event);
}

void ScrollableButton::ccTouchEnded(CCTouch *touch, CCEvent *event) {
  CCControlButton::ccTouchEnded(touch, event);
  dragging = false;
}

void ScrollableButton::ccTouchCancelled(CCTouch *touch, CCEvent *event) {
  CCControlButton::ccTouchCancelled(touch, event);
  dragging = false;
}

秘密酱是isTouchInside功能的覆盖,即使触摸在里面但被移动,它也会返回false。这样,一旦开始滚动,该按钮也将释放其“缩放”状态。

它还增加了一个小的容差因子,因此如果触摸移动一点点,它仍然被认为是“点击”。在上面的例子中,这个因子在25处被硬编码。

答案 1 :(得分:2)

我的问题: 有一个带有许多按钮(项目)的scrollView。它上面有2个功能按钮(返回,开始)。

  1. 当我向下滚动项目按钮覆盖功能按钮时。当我吞下滚动视图上方的所有触摸时,我将丢失我的功能按钮。所以我必须找到另一种解决方案。

  2. 当我开始拖动滚动视图时,按下了一个项目按钮。当我结束时,按钮动作将被执行。这非常烦人。

  3. 但是有解决方案。我创建了新的CCControlButton。它会检查是否在scrollview外部单击或被拖动。该按钮用于项目按钮。

    bool ControlButtonForScrolling::checkIfTouchIsInsideScrollView(CCTouch *pTouch)
    {
        CCPoint touchLocation = pTouch->getLocation(); // Get the touch position
        touchLocation = _scrollView->getParent()->convertToNodeSpace(touchLocation);
        CCRect bBox=_scrollView->boundingBox();
        bool result = bBox.containsPoint(touchLocation);
        return result;
    }
    
    bool ControlButtonForScrolling::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
    {
        bool isInside = this->checkIfTouchIsInsideScrollView(pTouch);
        if (isInside) {
            return CCControlButton::ccTouchBegan(pTouch, pEvent);
        }
        else
        {
            return false;
        }
    }
    
    void ControlButtonForScrolling::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
    {
        CCControlButton::ccTouchMoved(pTouch, pEvent);
        _scrollWasDragged = true; // information about dragging is stored to prevent sending action
    }
    
    void ControlButtonForScrolling::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
    {
        // this method is a copy of CCControlButton::ccTouchEnded except lines with _scrollWasDragged
        m_eState = CCControlStateNormal;
        m_isPushed = false;
        setHighlighted(false);
    
        if (!_scrollWasDragged)
        {
            if (isTouchInside(pTouch))
            {
                sendActionsForControlEvents(CCControlEventTouchUpInside);
            }
            else
            {
                sendActionsForControlEvents(CCControlEventTouchUpOutside);
            }
        }
        _scrollWasDragged = false;
    }