diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index f27225a67d..4dc6db8267 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -136,6 +136,10 @@ void ScrollView::onEnter() { Layout::onEnter(); scheduleUpdate(); + _eventListener = EventListenerFocus::create(); + _eventListener->onFocusChanged = CC_CALLBACK_2(ScrollView::onFocusChanged, this); + + _eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1); } bool ScrollView::init() @@ -1771,6 +1775,24 @@ Widget* ScrollView::findNextFocusedWidget(cocos2d::ui::Widget::FocusDirection di return Widget::findNextFocusedWidget(direction, current); } } + +void ScrollView::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus) +{ + if (dynamic_cast(widgetGetFocus) || dynamic_cast(widgetLostFocus)) { + return; + } + if (this->getLayoutType() == Layout::Type::VERTICAL) { + float loseFocusWidgetBoundary = widgetLostFocus->getTopInParent(); + float getFocusWidgetBoundary = widgetGetFocus->getTopInParent(); + + if (loseFocusWidgetBoundary >= getFocusWidgetBoundary) { + CCLOG("down"); + } + else{ + CCLOG("up"); + } + } +} } diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index c2c71309da..aeeb7b7ff9 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -30,6 +30,8 @@ THE SOFTWARE. NS_CC_BEGIN +class EventFocusListener; + namespace ui { class ScrollInnerContainer : public Layout @@ -382,6 +384,7 @@ protected: void bounceRightEvent(); + virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus); protected: Layout* _innerContainer; @@ -430,7 +433,7 @@ protected: float _bounceOriginalSpeed; bool _inertiaScrollEnabled; - + EventListenerFocus *_eventListener; Ref* _scrollViewEventListener; #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))