1
0

MyScrollView.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. content: cc.Node,
  5. _lastLocation: null,
  6. },
  7. onLoad() {
  8. this.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
  9. if (this._lastLocation === null) {
  10. this._lastLocation = event.getStartLocation();
  11. }
  12. let currentLocation = event.getLocation();
  13. let moveY = currentLocation.y - this._lastLocation.y;
  14. let moveX = currentLocation.x - this._lastLocation.x;
  15. let sin = moveY / moveX;
  16. if (sin >= 1 || sin <= -1) {
  17. let durationY = currentLocation.y - this._lastLocation.y;
  18. this.content.y = this.content.y + durationY;
  19. if (this.content.y <= this.node.height / 2) {
  20. this.content.y = this.node.height / 2;
  21. } else if (this.content.y >= this.content.height - this.node.height / 2) {
  22. this.content.y = this.content.height - this.node.height / 2;
  23. }
  24. this._lastLocation = currentLocation;
  25. }
  26. }, this);
  27. this.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  28. this._lastLocation = null;
  29. });
  30. this.node.on(cc.Node.EventType.TOUCH_CANCEL, (event) => {
  31. this._lastLocation = null;
  32. });
  33. },
  34. start() {
  35. },
  36. // update (dt) {},
  37. });