Fix event handling issues in Button/ScrollView

This commit is contained in:
pandamicro
2016-01-10 03:29:06 +08:00
parent 5183000163
commit e0e741884d
2 changed files with 9 additions and 53 deletions
+3 -33
View File
@@ -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;
+6 -20
View File
@@ -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();
}