-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathswipe.js
44 lines (42 loc) · 1.49 KB
/
swipe.js
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
var Recognizer = require('./recognizer')
var swipeRecognizer = {
events: ['swipe', 'swipeleft', 'swiperight', 'swipeup', 'swipedown'],
getAngle: function (x, y ) {
return Math.atan2(y, x) * 180 / Math.PI
},
getDirection: function (x, y) {
if (Math.abs(x) >= Math.abs(y)) {
return x < 0 ? 'left' : 'right'
}
return y < 0 ? 'up' : 'down'
},
touchstart: function (event) {
Recognizer.start(event, avalon.noop)
},
touchmove: function (event) {
Recognizer.move(event, avalon.noop)
},
touchend: function (event) {
if(event.changedTouches.length !== 1){
return
}
Recognizer.end(event, function (pointer, touch) {
var isflick = (pointer.distance > 30 && pointer.distance / pointer.duration > 0.65)
if (isflick) {
var extra = {
deltaX : pointer.deltaX,
deltaY: pointer.deltaY,
touch: touch,
touchEvent: event,
direction: swipeRecognizer.getDirection(pointer.deltaX, pointer.deltaY),
isVertical: pointer.isVertical
}
var target = pointer.element
Recognizer.fire(target, 'swipe', extra)
Recognizer.fire(target, 'swipe' + extra.direction, extra)
}
})
}
}
swipeRecognizer.touchcancel = swipeRecognizer.touchend
Recognizer.add('swipe', swipeRecognizer)