dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 
function setTimeout(instance, cb, time) {
  if (time > 0) {
    var s = getDate().getTime()
    var fn = function () {
        if (getDate().getTime() - s > time) {
            cb && cb()
        } else
            instance.requestAnimationFrame(fn)
    }
    fn()
  }
  else
    cb && cb()
}
 
// 判断触摸的移动方向
function decideSwiperDirection(startTouches, currentTouches, vertical) {
  // 震动偏移容差
  var toleranceShake = 150
  // 移动容差
  var toleranceTranslate = 10
  
  if (!vertical) {
    // 水平方向移动
    if (Math.abs(currentTouches.y - startTouches.y) <= toleranceShake) {
      // console.log(currentTouches.x, startTouches.x);
      if (Math.abs(currentTouches.x - startTouches.x) > toleranceTranslate) {
        if (currentTouches.x - startTouches.x > 0) {
          return 'right'
        } else if (currentTouches.x - startTouches.x < 0) {
          return 'left'
        }
      }
    }
  } else {
    // 垂直方向移动
    if (Math.abs(currentTouches.x - startTouches.x) <= toleranceShake) {
      // console.log(currentTouches.x, startTouches.x);
      if (Math.abs(currentTouches.y - startTouches.y) > toleranceTranslate) {
        if (currentTouches.y - startTouches.y > 0) {
          return 'down'
        } else if (currentTouches.y - startTouches.y < 0) {
          return 'up'
        }
      }
    }
  }
  return ''
}
 
// swiperItem参数数据更新
var itemDataObserver = function(newVal, oldVal, ownerInstance, instance) {
  if (!newVal || newVal === 'undefined') return
  var state = ownerInstance.getState()
  state.itemData = newVal
}
 
// swiperIndex数据更新
var currentIndexObserver = function(newVal, oldVal, ownerInstance, instance) {
  if ((!newVal && newVal != 0) || newVal === 'undefined') return
  var state = ownerInstance.getState()
  state.currentIndex = newVal
}
 
// containerData数据更新
var containerDataObserver = function(newVal, oldVal, ownerInstance, instance) {
  if (!newVal || newVal === 'undefined') return
  var state = ownerInstance.getState()
  state.containerData = newVal
}
 
// 开始触摸
var touchStart = function(event, ownerInstance) {
  console.log('touchStart');
  var instance = event.instance
  var dataset = instance.getDataset()
  var state = ownerInstance.getState()
  var itemData = state.itemData
  var containerData = state.containerData
  
  // 由于当前SwiperIndex初始为0,可能会导致swiperIndex数据没有更新
  if (!state.currentIndex || state.currentIndex === 'undefined') {
    state.currentIndex = 0
  }
  
  if (!containerData || containerData.circular === 'undefined') {
    containerData.circular = false
  }
  state.containerData = containerData
  
  // 如果当前切换动画还没执行结束,再次触摸会重新加载对应的swiperContainer的信息
  // console.log(containerData.animationFinish);
  if (!containerData.animationFinish) {
    ownerInstance.callMethod('changeParentSwiperContainerStyleStatus',{
      status: 'reload'
    })
  }
 
  // 判断是否为为当前显示的SwiperItem
  if (itemData.index != state.currentIndex) return
  
  var touches = event.changedTouches[0]
  if (!touches) return
  
  // 标记滑动开始时间
  state.touchStartTime = getDate().getTime()
  
  // 记录当前滑动开始的x,y坐标
  state.touchRelactive = {
    x: touches.pageX,
    y: touches.pageY
  }
  // 记录触摸id,用于处理多指的情况
  state.touchId = touches.identifier
  
  // 标记开始触摸
  state.touching = true
  ownerInstance.callMethod('updateTouchingStatus', {
    status: true
  })
}
 
// 正在移动
var touchMove = function(event, ownerInstance) {
  console.log('touchMove');
  var instance = event.instance
  var dataset = instance.getDataset()
  var state = ownerInstance.getState()
  var itemData = state.itemData
  var containerData = state.containerData
  
  // 判断是否为为当前显示的SwiperItem
  if (itemData.index != state.currentIndex) return
  
  // 判断是否开始触摸
  if (!state.touching) return
  
  var touches = event.changedTouches[0]
  if (!touches) return
  // 判断是否为同一个触摸点
  if (state.touchId != touches.identifier) return
  
  var currentTouchRelactive = {
    x: touches.pageX,
    y: touches.pageY
  }
  
  // 计算相对位移比例
  if (containerData.vertical) {
    var touchDistance = currentTouchRelactive.y - state.touchRelactive.y
    var itemHeight = itemData.itemHeight
    var distanceRate = touchDistance / itemHeight
    // console.log(currentTouchRelactive.y, touchDistance, itemHeight, distanceRate);
    
    // 判断是否为衔接轮播,如果不是衔接轮播,如果当前为第一个swiperItem并且向下滑、当前为最后一个swiperItem并且向上滑时不进行操作
    if (!containerData.circular &&
      ((state.currentIndex === 0 && touchDistance > 0) || (state.currentIndex === containerData.swiperItemLength - 1 && touchDistance < 0))
    ) {
      return
    }
    
    // 如果超出了距离则不进行操作
    if((Math.abs(touchDistance) > (itemData.itemTop + itemData.itemHeight))) {
      ownerInstance.callMethod('updateParentSwiperContainerStyle', {
        value: distanceRate < 0 ? -1 : 1
      })
      return
    }
  } else {
    var touchDistance = currentTouchRelactive.x - state.touchRelactive.x
    var itemWidth = itemData.itemWidth
    var distanceRate = touchDistance / itemWidth
    // console.log(currentTouchRelactive.x, touchDistance, itemWidth, distanceRate);
    
    // 判断是否为衔接轮播,如果不是衔接轮播,如果当前为第一个swiperItem并且向右滑、当前为最后一个swiperItem并且向左滑时不进行操作
    if (!containerData.circular &&
      ((state.currentIndex === 0 && touchDistance > 0) || (state.currentIndex === containerData.swiperItemLength - 1 && touchDistance < 0))
    ) {
      return
    }
    
    // 如果超出了距离则不进行操作
    if((Math.abs(touchDistance) > (itemData.itemLeft + itemData.itemWidth))) {
      ownerInstance.callMethod('updateParentSwiperContainerStyle', {
        value: distanceRate < 0 ? -1 : 1
      })
      return
    }
  }
  
  ownerInstance.callMethod('updateParentSwiperContainerStyle', {
    value: distanceRate
  })
}
 
// 移动结束
var touchEnd = function(event, ownerInstance) {
  console.log('touchEnd');
  var instance = event.instance
  var dataset = instance.getDataset()
  var state = ownerInstance.getState()
  var itemData = state.itemData
  var containerData = state.containerData
  
  // 判断是否为为当前显示的SwiperItem
  if (itemData.index != state.currentIndex) return
  
  // 判断是否开始触摸
  if (!state.touching) return
  
  var touches = event.changedTouches[0]
  if (!touches) return
  // 判断是否为同一个触摸点
  if (state.touchId != touches.identifier) return
  
  
  var currentTime = getDate().getTime()
  var currentTouchRelactive = {
    x: touches.pageX,
    y: touches.pageY
  }
  
  if (containerData.vertical) {
    // 判断触摸移动方向
    var direction = decideSwiperDirection(state.touchRelactive, currentTouchRelactive, true)
    // 判断是否为衔接轮播,如果不是衔接轮播,如果当前为第一个swiperItem并且向下滑、当前为最后一个swiperItem并且向上滑时不进行操作
    if (containerData.circular ||
      !((state.currentIndex === 0 && direction === 'down') || (state.currentIndex === containerData.swiperItemLength - 1 && direction === 'up'))
    ) {
      // 判断触摸的时间和移动的距离是否超过了当前itemHeight的一半,如果是则执行切换操作
      // console.log(currentTime - state.touchStartTime, Math.abs(currentTouchRelactive.y - state.touchRelactive.y));
      if ((currentTime - state.touchStartTime) > 200 && Math.abs(currentTouchRelactive.y - state.touchRelactive.y) < itemData.itemHeight / 2) {
        ownerInstance.callMethod('changeParentSwiperContainerStyleStatus',{
          status: 'reset'
        })
      } else {
        // console.log(direction, state.touchRelactive.y, currentTouchRelactive.y);
        
        ownerInstance.callMethod('updateParentSwiperContainerStyleWithDirection', {
          direction: direction
        })
      }
    }
  } else {
    // 判断触摸移动方向
    var direction = decideSwiperDirection(state.touchRelactive, currentTouchRelactive, false)
    // 判断是否为衔接轮播,如果不是衔接轮播,如果当前为第一个swiperItem并且向右滑、当前为最后一个swiperItem并且向左滑时不进行操作
    if (containerData.circular ||
      !((state.currentIndex === 0 && direction === 'right') || (state.currentIndex === containerData.swiperItemLength - 1 && direction === 'left'))
    ) {
      // 判断触摸的时间和移动的距离是否超过了当前itemWidth的一半,如果是则执行切换操作
      // console.log(currentTime - state.touchStartTime, Math.abs(currentTouchRelactive.x - state.touchRelactive.x));
      if ((currentTime - state.touchStartTime) > 200 && Math.abs(currentTouchRelactive.x - state.touchRelactive.x) < itemData.itemWidth / 2) {
        ownerInstance.callMethod('changeParentSwiperContainerStyleStatus',{
          status: 'reset'
        })
      } else {
        // console.log(direction, state.touchRelactive.x, currentTouchRelactive.x);
        
        ownerInstance.callMethod('updateParentSwiperContainerStyleWithDirection', {
          direction: direction
        })
      }
    }
  }
  
  // 清除标记
  state.touchId = null
  state.touchRelactive = null
  state.touchStartTime = 0
  
  
  // 标记停止触摸
  state.touching = true
  ownerInstance.callMethod('updateTouchingStatus', {
    status: false
  })
}
 
module.exports = {
  itemDataObserver: itemDataObserver,
  currentIndexObserver: currentIndexObserver,
  containerDataObserver: containerDataObserver,
  touchStart: touchStart,
  touchMove: touchMove,
  touchEnd: touchEnd
}