1

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

4

2 回答 2

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。

于 2013-07-27T17:49:51.083 回答
2

我的问题:有一个带有许多按钮(项目)的滚动视图。上面有 2 个功能按钮(返回、开始)。

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

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

但有解决办法。我创建了新的 CCControlButton。它检查是否在滚动视图之外单击或被拖动。该按钮用于项目按钮。

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;
}
于 2013-05-22T16:23:17.323 回答