From e0e741884d59ea04b5e41ab0fd3bd2f59bfbbbd9 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Sun, 10 Jan 2016 03:29:06 +0800 Subject: [PATCH] Fix event handling issues in Button/ScrollView --- cocos2d/core/components/CCButton.js | 36 +++---------------------- cocos2d/core/components/CCScrollView.js | 26 +++++------------- 2 files changed, 9 insertions(+), 53 deletions(-) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 2ea8c1b31f..d29b11a6da 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -334,7 +334,6 @@ var Button = cc.Class({ _registerEvent: function () { this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this); this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this); - this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this); this.node.on(cc.Node.EventType.MOUSE_ENTER, this._onMouseMoveIn, this); @@ -378,27 +377,10 @@ var Button = cc.Class({ // touch event handler _onTouchBegan: function (event) { - var touch = event.touch; - if (!this.interactable || !this.enabledInHierarchy) return false; + if (!this.interactable || !this.enabledInHierarchy) return; - var hit = this._hitTest(touch.getLocation()); - if (hit) { - this._pressed = true; - this._applyState(ButtonState.Pressed); - } - - return hit; - }, - - _onTouchMove: function (event) { - var touch = event.touch; - - var hit = this._hitTest(touch.getLocation()); - if (hit && this._pressed) { - this._applyState(ButtonState.Pressed); - }else{ - this._applyState(ButtonState.Normal); - } + this._pressed = true; + this._applyState(ButtonState.Pressed); }, _onTouchEnded: function () { @@ -465,18 +447,6 @@ var Button = cc.Class({ } }, - // hit test - _hitTest: function (pos) { - var target = this.target; - if (!target) return false; - - var w = target.width; - var h = target.height; - - var rect = cc.rect(0, 0, w, h); - return cc.rectContainsPoint(rect, target.convertToNodeSpace(pos)); - } - }); cc.Button = module.exports = Button; diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 365f5fb7b1..7ff29bf2aa 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -487,27 +487,11 @@ var ScrollView = cc.Class({ return contentParent.convertToNodeSpaceAR(scrollViewPositionInWorldSpace); }, - - _hitTest: function(pos) { - var target = this.node; - - var w = target.width; - var h = target.height; - var anchor = target.getAnchorPoint(); - - var rect = cc.rect(-anchor.x * w, -anchor.y * h, w, h); - return cc.rectContainsPoint(rect, target.convertToNodeSpaceAR(pos)); - }, - // touch event handler _onTouchBegan: function(event) { var touch = event.touch; - var hit = this._hitTest(touch.getLocation()); - if (hit && this.content) { - this._handlePressLogic(touch); - } - - return hit; + this._handlePressLogic(touch); + event.stopPropagation(); }, _cancelButtonClick: function(touch) { @@ -535,6 +519,7 @@ var ScrollView = cc.Class({ } this._handleMoveLogic(touch); } + event.stopPropagation(); }, _onTouchEnded: function(event) { @@ -542,13 +527,14 @@ var ScrollView = cc.Class({ if (this.content) { this._handleReleaseLogic(touch); } - + event.stopPropagation(); }, _onTouchCancelled: function(event) { var touch = event.touch; if(this.content){ this._handleReleaseLogic(touch); } + event.stopPropagation(); }, _handleMoveLogic: function(touch) { @@ -889,7 +875,7 @@ var ScrollView = cc.Class({ }, //component life cycle methods - onLoad: function() { + onLoad: function () { if (!CC_EDITOR) { this._registerEvent(); }