gui-touch.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view
  3. @touchstart="touchstart"
  4. @touchmove="touchmove"
  5. @touchend="touchend">
  6. <slot></slot>
  7. </view>
  8. </template>
  9. <script>
  10. export default{
  11. name : "gui-touch",
  12. props : {
  13. datas:{type:Array, default:function(){return [];}}
  14. },
  15. data() {
  16. return {
  17. toucheTimer : 0,
  18. fingerRes : [],
  19. distance : 0,
  20. taptimer : 100
  21. }
  22. },
  23. methods:{
  24. toInt : function(arr){
  25. var res = [];
  26. arr.forEach((item)=>{
  27. item.pageX = parseInt(item.pageX);
  28. item.pageY = parseInt(item.pageY);
  29. res.push(item);
  30. });
  31. return res;
  32. },
  33. touchstart : function(e){
  34. this.toucheTimer = new Date().getTime();
  35. this.fingerRes = this.toInt(e.changedTouches);
  36. if(this.fingerRes.length > 2){return ;}
  37. var moves = [], i = 0;
  38. this.fingerRes.forEach((finger)=>{
  39. var xTouch = finger.pageX;
  40. var yTouch = finger.pageY;
  41. moves.push([xTouch, yTouch]);
  42. i++;
  43. });
  44. this.$emit('thStart', moves, this.datas);
  45. },
  46. touchmove : function(e){
  47. if(this.toucheTimer < 50){return ;}
  48. var timer = new Date().getTime() - this.toucheTimer;
  49. if(timer < this.taptimer){return ;}
  50. var touches = this.toInt(e.changedTouches);
  51. if(touches.length > 2){return ;}
  52. if(touches.length == 1){
  53. var i = 0, moves = [];
  54. touches.forEach((finger)=>{
  55. var xTouch = finger.pageX - this.fingerRes[i].pageX;
  56. var yTouch = finger.pageY - this.fingerRes[i].pageY;
  57. moves.push([xTouch, yTouch]);
  58. i++;
  59. });
  60. this.$emit('thMove', moves, this.datas);
  61. }
  62. else if(touches.length == 2){
  63. if(this.distance == 0){
  64. this.distance = parseInt(this.getDistance(touches[0].pageX,touches[0].pageY, touches[1].pageX, touches[1].pageY));
  65. }else{
  66. var distance1 = parseInt(this.getDistance(touches[0].pageX,touches[0].pageY, touches[1].pageX, touches[1].pageY));
  67. var scale = distance1 / this.distance;
  68. scale = Math.floor(scale * 100) / 100;
  69. this.$emit('scale', scale, this.datas);
  70. }
  71. }
  72. },
  73. touchend : function (e){
  74. var timer = new Date().getTime() - this.toucheTimer;
  75. if(timer < this.taptimer){
  76. this.$emit('tapme');
  77. return ;
  78. }
  79. var touches = this.toInt(e.changedTouches);
  80. this.distance = 0;
  81. if(touches.length == 1){
  82. var i = 0, moves = [];
  83. touches.forEach((finger)=>{
  84. var xTouch = finger.pageX - this.fingerRes[i].pageX;
  85. var yTouch = finger.pageY - this.fingerRes[i].pageY;
  86. moves.push([xTouch, yTouch]);
  87. i++;
  88. });
  89. moves.push(timer);
  90. this.$emit('thEnd', moves, this.datas);
  91. // 根据时间及距离决定滑动时间
  92. if(timer < 300){
  93. var mx = Math.abs(moves[0][0]);
  94. var my = Math.abs(moves[0][1]);
  95. if(mx > my){
  96. if(mx >= 50){
  97. if(moves[0][0] > 0){
  98. this.$emit('swipe', 'right', this.datas);
  99. }else{
  100. this.$emit('swipe', 'left', this.datas);
  101. }
  102. }
  103. }else{
  104. if(my >= 50){
  105. if(moves[0][1] > 0){
  106. this.$emit('swipe', 'down', this.datas);
  107. }else{
  108. this.$emit('swipe', 'up', this.datas);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. },
  115. getDistance : function (lat1, lng1, lat2, lng2){
  116. var radLat1 = lat1*Math.PI / 180.0;
  117. var radLat2 = lat2*Math.PI / 180.0;
  118. var a = radLat1 - radLat2;
  119. var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
  120. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
  121. s = s * 6378.137;
  122. return Math.round(s * 10000) / 10000;
  123. },
  124. tapme : function(){
  125. this.isTap = true;
  126. }
  127. }
  128. }
  129. </script>
  130. <style scoped></style>