| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c5db112 commit b8a82f2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,43 +7,49 @@ import * as stackLayoutModule from "tns-core-modules/ui/layouts/stack-layout"; | |||
| 7 | 7 | ||
| 8 | 8 | export function createPage() { | |
| 9 | 9 | ||
| 10 | - var stack = new stackLayoutModule.StackLayout(); | ||
| 11 | - var labelHeight = Math.round(deviceProperties.screen.mainScreen.heightPixels / (7 * deviceProperties.screen.mainScreen.scale)); | ||
| 12 | - var stopButton = new button.Button(); | ||
| 10 | + const stack = new stackLayoutModule.StackLayout(); | ||
| 11 | + const labelHeight = Math.round(deviceProperties.screen.mainScreen.heightPixels / (7 * deviceProperties.screen.mainScreen.scale)); | ||
| 12 | + const stopButton = new button.Button(); | ||
| 13 | 13 | stopButton.text = "Stop Detecting Gestures"; | |
| 14 | 14 | stack.addChild(stopButton); | |
| 15 | 15 | ||
| 16 | - var tapLabel = new labelModule.Label(); | ||
| 16 | + const tapLabel = new labelModule.Label(); | ||
| 17 | 17 | tapLabel.text = "Tap here"; | |
| 18 | 18 | stack.addChild(tapLabel); | |
| 19 | 19 | ||
| 20 | - var doubletapLabel = new labelModule.Label(); | ||
| 20 | + const doubletapLabel = new labelModule.Label(); | ||
| 21 | 21 | doubletapLabel.text = "Double Tap here"; | |
| 22 | 22 | stack.addChild(doubletapLabel); | |
| 23 | 23 | ||
| 24 | - var longpressLabel = new labelModule.Label(); | ||
| 24 | + const longpressLabel = new labelModule.Label(); | ||
| 25 | 25 | longpressLabel.text = "Long Press here"; | |
| 26 | 26 | stack.addChild(longpressLabel); | |
| 27 | 27 | ||
| 28 | - var swipeLabel = new labelModule.Label(); | ||
| 28 | + const tapAndDoubleTapLabel = new labelModule.Label(); | ||
| 29 | + tapAndDoubleTapLabel.height = labelHeight; | ||
| 30 | + tapAndDoubleTapLabel.text = "Tap or Double Tap"; | ||
| 31 | + tapAndDoubleTapLabel.textWrap = true; | ||
| 32 | + stack.addChild(tapAndDoubleTapLabel); | ||
| 33 | + | ||
| 34 | + const swipeLabel = new labelModule.Label(); | ||
| 29 | 35 | swipeLabel.height = labelHeight; | |
| 30 | 36 | swipeLabel.text = "Swipe here"; | |
| 31 | 37 | swipeLabel.textWrap = true; | |
| 32 | 38 | stack.addChild(swipeLabel); | |
| 33 | 39 | ||
| 34 | - var panLabel = new labelModule.Label(); | ||
| 40 | + const panLabel = new labelModule.Label(); | ||
| 35 | 41 | panLabel.height = labelHeight; | |
| 36 | 42 | panLabel.text = "Pan here"; | |
| 37 | 43 | panLabel.textWrap = true; | |
| 38 | 44 | stack.addChild(panLabel); | |
| 39 | 45 | ||
| 40 | - var pinchLabel = new labelModule.Label(); | ||
| 46 | + const pinchLabel = new labelModule.Label(); | ||
| 41 | 47 | pinchLabel.height = labelHeight; | |
| 42 | 48 | pinchLabel.text = "Pinch here"; | |
| 43 | 49 | pinchLabel.textWrap = true; | |
| 44 | 50 | stack.addChild(pinchLabel); | |
| 45 | 51 | ||
| 46 | - var rotaionLabel = new labelModule.Label(); | ||
| 52 | + const rotaionLabel = new labelModule.Label(); | ||
| 47 | 53 | rotaionLabel.height = labelHeight; | |
| 48 | 54 | rotaionLabel.text = "Rotate here"; | |
| 49 | 55 | rotaionLabel.textWrap = true; | |
@@ -57,63 +63,78 @@ export function createPage() { | |||
| 57 | 63 | observer5.disconnect(); | |
| 58 | 64 | observer6.disconnect(); | |
| 59 | 65 | observer7.disconnect(); | |
| 66 | + observer8.disconnect(); | ||
| 67 | + observer9.disconnect(); | ||
| 60 | 68 | tapLabel.text = "Gestures detection disabled"; | |
| 61 | 69 | doubletapLabel.text = "Gestures detection disabled"; | |
| 62 | 70 | longpressLabel.text = "Gestures detection disabled"; | |
| 63 | 71 | swipeLabel.text = "Gesturesd detection disabled"; | |
| 64 | 72 | panLabel.text = "Gestures detection disabled"; | |
| 65 | 73 | pinchLabel.text = "Gestures detection disabled"; | |
| 66 | 74 | rotaionLabel.text = "Gestures detection disabled"; | |
| 75 | + tapAndDoubleTapLabel.text = "Gestures detection disabled"; | ||
| 67 | 76 | }); | |
| 68 | 77 | ||
| 69 | 78 | tapLabel.on(gestures.GestureTypes.tap, function (args: gestures.GestureEventData) { | |
| 70 | 79 | tapLabel.text = "Tap gesture detected, " + (args.object === tapLabel); | |
| 71 | 80 | }); | |
| 72 | 81 | ||
| 73 | - var observer1 = tapLabel.getGestureObservers(gestures.GestureTypes.tap)[0]; | ||
| 82 | + const observer1 = tapLabel.getGestureObservers(gestures.GestureTypes.tap)[0]; | ||
| 74 | 83 | ||
| 75 | 84 | doubletapLabel.on(gestures.GestureTypes.doubleTap, function (args: gestures.GestureEventData) { | |
| 76 | 85 | doubletapLabel.text = "Double Tap gesture detected, " + (args.object === doubletapLabel); | |
| 77 | 86 | }); | |
| 78 | 87 | ||
| 79 | - var observer2 = doubletapLabel.getGestureObservers(gestures.GestureTypes.doubleTap)[0]; | ||
| 88 | + const observer2 = doubletapLabel.getGestureObservers(gestures.GestureTypes.doubleTap)[0]; | ||
| 80 | 89 | ||
| 81 | 90 | longpressLabel.on(gestures.GestureTypes.longPress, function (args: gestures.GestureEventData) { | |
| 82 | 91 | longpressLabel.text = "Long Press gesture detected, " + (args.object === longpressLabel); | |
| 83 | 92 | }); | |
| 84 | 93 | ||
| 85 | - var observer3 = longpressLabel.getGestureObservers(gestures.GestureTypes.longPress)[0]; | ||
| 94 | + const observer3 = longpressLabel.getGestureObservers(gestures.GestureTypes.longPress)[0]; | ||
| 86 | 95 | ||
| 87 | 96 | swipeLabel.on(gestures.GestureTypes.swipe, function (args: gestures.SwipeGestureEventData) { | |
| 88 | 97 | swipeLabel.text = "Swipe Direction: " + args.direction + ", " + (args.object === swipeLabel); // + getStateAsString(args.state); | |
| 89 | 98 | }); | |
| 90 | 99 | ||
| 91 | - var observer4 = swipeLabel.getGestureObservers(gestures.GestureTypes.swipe)[0]; | ||
| 100 | + const observer4 = swipeLabel.getGestureObservers(gestures.GestureTypes.swipe)[0]; | ||
| 92 | 101 | ||
| 93 | 102 | panLabel.on(gestures.GestureTypes.pan, function (args: gestures.PanGestureEventData) { | |
| 94 | 103 | panLabel.text = "Pan deltaX:" + Math.round(args.deltaX) + "; deltaY:" + Math.round(args.deltaY) + ";" + ", " + (args.object === panLabel) + getStateAsString(args.state); | |
| 95 | 104 | }); | |
| 96 | 105 | ||
| 97 | - var observer5 = panLabel.getGestureObservers(gestures.GestureTypes.pan)[0]; | ||
| 106 | + const observer5 = panLabel.getGestureObservers(gestures.GestureTypes.pan)[0]; | ||
| 98 | 107 | ||
| 99 | 108 | pinchLabel.on(gestures.GestureTypes.pinch, function (args: gestures.PinchGestureEventData) { | |
| 100 | 109 | pinchLabel.text = "Pinch Scale: " + Math.round(args.scale) + ", " + (args.object === pinchLabel) + getStateAsString(args.state); | |
| 101 | 110 | }); | |
| 102 | 111 | ||
| 103 | - var observer6 = pinchLabel.getGestureObservers(gestures.GestureTypes.pinch)[0]; | ||
| 112 | + const observer6 = pinchLabel.getGestureObservers(gestures.GestureTypes.pinch)[0]; | ||
| 104 | 113 | ||
| 105 | 114 | rotaionLabel.on(gestures.GestureTypes.rotation, function (args: gestures.RotationGestureEventData) { | |
| 106 | 115 | rotaionLabel.text = "Rotation: " + Math.round(args.rotation) + ", " + (args.object === rotaionLabel) + getStateAsString(args.state); | |
| 107 | 116 | }); | |
| 108 | 117 | ||
| 109 | - var observer7 = rotaionLabel.getGestureObservers(gestures.GestureTypes.rotation)[0]; | ||
| 118 | + const observer7 = rotaionLabel.getGestureObservers(gestures.GestureTypes.rotation)[0]; | ||
| 119 | + | ||
| 120 | + tapAndDoubleTapLabel.on(gestures.GestureTypes.doubleTap, function (args: gestures.GestureEventData) { | ||
| 121 | + tapAndDoubleTapLabel.text = "Last action: Double tap gesture, " + (args.object === tapAndDoubleTapLabel); | ||
| 122 | + }); | ||
| 123 | + | ||
| 124 | + const observer8 = tapAndDoubleTapLabel.getGestureObservers(gestures.GestureTypes.doubleTap)[0]; | ||
| 125 | + | ||
| 126 | + tapAndDoubleTapLabel.on(gestures.GestureTypes.tap, function (args: gestures.GestureEventData) { | ||
| 127 | + tapAndDoubleTapLabel.text = "Last action: Tap gesture, " + (args.object === tapAndDoubleTapLabel); | ||
| 128 | + }); | ||
| 129 | + | ||
| 130 | + const observer9 = tapAndDoubleTapLabel.getGestureObservers(gestures.GestureTypes.tap)[0]; | ||
| 110 | 131 | ||
| 111 | - var page = new pages.Page(); | ||
| 132 | + const page = new pages.Page(); | ||
| 112 | 133 | page.content = stack; | |
| 113 | 134 | return page; | |
| 114 | 135 | } | |
| 115 | 136 | ||
| 116 | - var states = new Array<string>(); | ||
| 137 | + const states = new Array<string>(); | ||
| 117 | 138 | function getStateAsString(state: gestures.GestureStateTypes): string { | |
| 118 | 139 | if (state === gestures.GestureStateTypes.began) { | |
| 119 | 140 | states.length = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ import { | |||
| 10 | 10 | // Import layout from utils directly to avoid circular references | |
| 11 | 11 | import { layout } from "../../utils/utils"; | |
| 12 | 12 | ||
| 13 | + import * as timer from "../../timer"; | ||
| 14 | + | ||
| 13 | 15 | export * from "./gestures-common"; | |
| 14 | 16 | ||
| 15 | 17 | interface TapAndDoubleTapGestureListener { | |
@@ -27,6 +29,11 @@ function initializeTapAndDoubleTapGestureListener() { | |||
| 27 | 29 | private _target: View; | |
| 28 | 30 | private _type: number; | |
| 29 | 31 | ||
| 32 | + private _lastUpTime: number = 0; | ||
| 33 | + private _tapTimeoutId: number; | ||
| 34 | + | ||
| 35 | + private static DoubleTapTimeout = android.view.ViewConfiguration.getDoubleTapTimeout(); | ||
| 36 | + | ||
| 30 | 37 | constructor(observer: GesturesObserver, target: View, type: number) { | |
| 31 | 38 | super(); | |
| 32 | 39 | ||
@@ -37,28 +44,42 @@ function initializeTapAndDoubleTapGestureListener() { | |||
| 37 | 44 | } | |
| 38 | 45 | ||
| 39 | 46 | public onSingleTapUp(motionEvent: android.view.MotionEvent): boolean { | |
| 40 | - if (this._type & GestureTypes.tap) { | ||
| 41 | - let args = _getArgs(GestureTypes.tap, this._target, motionEvent); | ||
| 42 | - _executeCallback(this._observer, args); | ||
| 47 | + this._handleSingleTap(motionEvent); | ||
| 48 | + this._lastUpTime = Date.now(); | ||
| 49 | + return true; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public onDown(motionEvent: android.view.MotionEvent): boolean { | ||
| 53 | + const tapTime = Date.now(); | ||
| 54 | + if ((tapTime - this._lastUpTime) <= TapAndDoubleTapGestureListenerImpl.DoubleTapTimeout) { | ||
| 55 | + this._handleDoubleTap(motionEvent); | ||
| 43 | 56 | } | |
| 44 | 57 | return true; | |
| 45 | 58 | } | |
| 46 | 59 | ||
| 47 | - public onDoubleTap(motionEvent: android.view.MotionEvent): boolean { | ||
| 48 | - if (this._type & GestureTypes.doubleTap) { | ||
| 49 | - let args = _getArgs(GestureTypes.doubleTap, this._target, motionEvent); | ||
| 60 | + public onLongPress(motionEvent: android.view.MotionEvent): void { | ||
| 61 | + if (this._type & GestureTypes.longPress) { | ||
| 62 | + const args = _getArgs(GestureTypes.longPress, this._target, motionEvent); | ||
| 50 | 63 | _executeCallback(this._observer, args); | |
| 51 | 64 | } | |
| 52 | - return true; | ||
| 53 | 65 | } | |
| 54 | 66 | ||
| 55 | - public onDown(motionEvent: android.view.MotionEvent): boolean { | ||
| 56 | - return true; | ||
| 67 | + private _handleSingleTap(motionEvent: android.view.MotionEvent): void { | ||
| 68 | + this._tapTimeoutId = timer.setTimeout(() => { | ||
| 69 | + if (this._type & GestureTypes.tap) { | ||
| 70 | + const args = _getArgs(GestureTypes.tap, this._target, motionEvent); | ||
| 71 | + _executeCallback(this._observer, args); | ||
| 72 | + } | ||
| 73 | + timer.clearTimeout(this._tapTimeoutId); | ||
| 74 | + }, TapAndDoubleTapGestureListenerImpl.DoubleTapTimeout); | ||
| 57 | 75 | } | |
| 58 | 76 | ||
| 59 | - public onLongPress(motionEvent: android.view.MotionEvent): void { | ||
| 60 | - if (this._type & GestureTypes.longPress) { | ||
| 61 | - let args = _getArgs(GestureTypes.longPress, this._target, motionEvent); | ||
| 77 | + private _handleDoubleTap(motionEvent: android.view.MotionEvent): void { | ||
| 78 | + if (this._tapTimeoutId) { | ||
| 79 | + timer.clearTimeout(this._tapTimeoutId); | ||
| 80 | + } | ||
| 81 | + if (this._type & GestureTypes.doubleTap) { | ||
| 82 | + const args = _getArgs(GestureTypes.doubleTap, this._target, motionEvent); | ||
| 62 | 83 | _executeCallback(this._observer, args); | |
| 63 | 84 | } | |
| 64 | 85 | } | |
@@ -94,7 +115,7 @@ function initializePinchGestureListener() { | |||
| 94 | 115 | public onScaleBegin(detector: android.view.ScaleGestureDetector): boolean { | |
| 95 | 116 | this._scale = detector.getScaleFactor(); | |
| 96 | 117 | ||
| 97 | - let args = new PinchGestureEventData( | ||
| 118 | + const args = new PinchGestureEventData( | ||
| 98 | 119 | this._target, | |
| 99 | 120 | detector, | |
| 100 | 121 | this._scale, | |
@@ -109,7 +130,7 @@ function initializePinchGestureListener() { | |||
| 109 | 130 | public onScale(detector: android.view.ScaleGestureDetector): boolean { | |
| 110 | 131 | this._scale *= detector.getScaleFactor(); | |
| 111 | 132 | ||
| 112 | - let args = new PinchGestureEventData( | ||
| 133 | + const args = new PinchGestureEventData( | ||
| 113 | 134 | this._target, | |
| 114 | 135 | detector, | |
| 115 | 136 | this._scale, | |
@@ -123,7 +144,7 @@ function initializePinchGestureListener() { | |||
| 123 | 144 | public onScaleEnd(detector: android.view.ScaleGestureDetector): void { | |
| 124 | 145 | this._scale *= detector.getScaleFactor(); | |
| 125 | 146 | ||
| 126 | - let args = new PinchGestureEventData( | ||
| 147 | + const args = new PinchGestureEventData( | ||
| 127 | 148 | this._target, | |
| 128 | 149 | detector, | |
| 129 | 150 | this._scale, | |
@@ -172,41 +193,28 @@ function initializeSwipeGestureListener() { | |||
| 172 | 193 | let deltaX = currentEvent.getX() - initialEvent.getX(); | |
| 173 | 194 | ||
| 174 | 195 | if (Math.abs(deltaX) > Math.abs(deltaY)) { | |
| 175 | - | ||
| 176 | 196 | if (Math.abs(deltaX) > SWIPE_THRESHOLD | |
| 177 | 197 | && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { | |
| 178 | - | ||
| 179 | 198 | if (deltaX > 0) { | |
| 180 | - | ||
| 181 | 199 | args = _getSwipeArgs(SwipeDirection.right, this._target, initialEvent, currentEvent); | |
| 182 | 200 | _executeCallback(this._observer, args); | |
| 183 | - | ||
| 184 | 201 | result = true; | |
| 185 | 202 | } else { | |
| 186 | - | ||
| 187 | 203 | args = _getSwipeArgs(SwipeDirection.left, this._target, initialEvent, currentEvent); | |
| 188 | 204 | _executeCallback(this._observer, args); | |
| 189 | - | ||
| 190 | 205 | result = true; | |
| 191 | 206 | } | |
| 192 | 207 | } | |
| 193 | - | ||
| 194 | 208 | } else { | |
| 195 | - | ||
| 196 | 209 | if (Math.abs(deltaY) > SWIPE_THRESHOLD | |
| 197 | 210 | && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
| 198 | - | ||
| 199 | 211 | if (deltaY > 0) { | |
| 200 | - | ||
| 201 | 212 | args = _getSwipeArgs(SwipeDirection.down, this._target, initialEvent, currentEvent); | |
| 202 | 213 | _executeCallback(this._observer, args); | |
| 203 | - | ||
| 204 | 214 | result = true; | |
| 205 | 215 | } else { | |
| 206 | - | ||
| 207 | 216 | args = _getSwipeArgs(SwipeDirection.up, this._target, initialEvent, currentEvent); | |
| 208 | 217 | _executeCallback(this._observer, args); | |
| 209 | - | ||
| 210 | 218 | result = true; | |
| 211 | 219 | } | |
| 212 | 220 | } | |
@@ -228,7 +236,7 @@ const INVALID_POINTER_ID = -1; | |||
| 228 | 236 | const TO_DEGREES = (180 / Math.PI); | |
| 229 | 237 | ||
| 230 | 238 | export function observe(target: View, type: GestureTypes, callback: (args: GestureEventData) => void, context?: any): GesturesObserver { | |
| 231 | - let observer = new GesturesObserver(target, callback, context); | ||
| 239 | + const observer = new GesturesObserver(target, callback, context); | ||
| 232 | 240 | observer.observe(type); | |
| 233 | 241 | return observer; | |
| 234 | 242 | } | |
@@ -292,7 +300,7 @@ export class GesturesObserver extends GesturesObserverBase { | |||
| 292 | 300 | private _attach(target: View, type: GestureTypes) { | |
| 293 | 301 | this._detach(); | |
| 294 | 302 | ||
| 295 | - if (type & GestureTypes.tap || type & GestureTypes.doubleTap || type & GestureTypes.longPress) { | ||
| 303 | + if ((type & GestureTypes.tap) || (type & GestureTypes.doubleTap) || (type & GestureTypes.longPress)) { | ||
| 296 | 304 | initializeTapAndDoubleTapGestureListener(); | |
| 297 | 305 | this._simpleGestureDetector = new androidx.core.view.GestureDetectorCompat(target._context, new TapAndDoubleTapGestureListener(this, this.target, type)); | |
| 298 | 306 | } | |
@@ -465,7 +473,7 @@ class CustomPanGestureDetector { | |||
| 465 | 473 | return true; | |
| 466 | 474 | } | |
| 467 | 475 | ||
| 468 | - private trackStop(currentEvent: android.view.MotionEvent, cahceEvent: boolean) { | ||
| 476 | + private trackStop(currentEvent: android.view.MotionEvent, cacheEvent: boolean) { | ||
| 469 | 477 | if (this.isTracking) { | |
| 470 | 478 | let args = _getPanArgs(this.deltaX, this.deltaY, this.target, GestureStateTypes.ended, null, currentEvent); | |
| 471 | 479 | _executeCallback(this.observer, args); | |
@@ -475,10 +483,9 @@ class CustomPanGestureDetector { | |||
| 475 | 483 | this.isTracking = false; | |
| 476 | 484 | } | |
| 477 | 485 | ||
| 478 | - if (cahceEvent) { | ||
| 486 | + if (cacheEvent) { | ||
| 479 | 487 | this.lastEventCache = currentEvent; | |
| 480 | - } | ||
| 481 | - else { | ||
| 488 | + } else { | ||
| 482 | 489 | this.lastEventCache = undefined; | |
| 483 | 490 | } | |
| 484 | 491 | } | |
@@ -509,8 +516,7 @@ class CustomPanGestureDetector { | |||
| 509 | 516 | x: event.getRawX() / this.density, | |
| 510 | 517 | y: event.getRawY() / this.density | |
| 511 | 518 | }; | |
| 512 | - } | ||
| 513 | - else { | ||
| 519 | + } else { | ||
| 514 | 520 | const offX = event.getRawX() - event.getX(); | |
| 515 | 521 | const offY = event.getRawY() - event.getY(); | |
| 516 | 522 | let res = { x: 0, y: 0 }; | |
@@ -559,8 +565,7 @@ class CustomRotateGestureDetector { | |||
| 559 | 565 | if (this.trackedPtrId1 === INVALID_POINTER_ID && pointerID !== this.trackedPtrId2) { | |
| 560 | 566 | this.trackedPtrId1 = pointerID; | |
| 561 | 567 | assigned = true; | |
| 562 | - } | ||
| 563 | - else if (this.trackedPtrId2 === INVALID_POINTER_ID && pointerID !== this.trackedPtrId1) { | ||
| 568 | + } else if (this.trackedPtrId2 === INVALID_POINTER_ID && pointerID !== this.trackedPtrId1) { | ||
| 564 | 569 | this.trackedPtrId2 = pointerID; | |
| 565 | 570 | assigned = true; | |
| 566 | 571 | } | |
@@ -585,8 +590,7 @@ class CustomRotateGestureDetector { | |||
| 585 | 590 | case android.view.MotionEvent.ACTION_POINTER_UP: | |
| 586 | 591 | if (pointerID === this.trackedPtrId1) { | |
| 587 | 592 | this.trackedPtrId1 = INVALID_POINTER_ID; | |
| 588 | - } | ||
| 589 | - else if (pointerID === this.trackedPtrId2) { | ||
| 593 | + } else if (pointerID === this.trackedPtrId2) { | ||
| 590 | 594 | this.trackedPtrId2 = INVALID_POINTER_ID; | |
| 591 | 595 | } | |
| 592 | 596 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments