[英]Determine whether a swipe event is for a left swipe or a right swipe
I am using Angular 2 and ionic 2 and am trying to capture left and right swipes.我正在使用 Angular 2 和 ionic 2,并试图捕捉左右滑动。 I can capture a swipe but can't work out how to determine if it is a left or right swipe.我可以捕获滑动,但无法确定如何确定它是向左滑动还是向右滑动。
In my html I have:在我的 html 中,我有:
<ion-content (swipe)="swipeEvent($event)">
This calls swipeEvent
every time a swipe occurs.每次发生滑动时都会调用swipeEvent
。
My swipeEvent
javascript code looks like this:我的swipeEvent
javascript 代码如下所示:
swipeEvent(event){
alert('swipe');
}
How can I determine if it is a left or right swipe.如何确定是向左滑动还是向右滑动。
It looks like the gestures are built on top of HammerJS as stated in the Ionic 2 docs . 看起来手势是建立在HammerJS之上的,如Ionic 2文档中所述 。
You can target specific gestures to trigger specific functionality. 您可以定位特定手势以触发特定功能。 Built on top of Hammer.js... 建立在Hammer.js之上......
When you trigger a swipe event an object gets passed to the bound method it contains an option e.direction
which is a numeric value corresponding to a swipe direction. 当您触发滑动事件时,对象将传递给绑定方法,它包含一个选项e.direction
,它是一个与滑动方向对应的数值。
Below is the list of direction constants which are defined here in the HammerJS docs 下面是其定义方向常量列表这里的HammerJS文档
Name Value DIRECTION_NONE 1 DIRECTION_LEFT 2 DIRECTION_RIGHT 4 DIRECTION_UP 8 DIRECTION_DOWN 16 DIRECTION_HORIZONTAL 6 DIRECTION_VERTICAL 24 DIRECTION_ALL 30
Example 例
Given your ion-content
setup 鉴于您的ion-content
设置
swipeEvent(e) {
if (e.direction == 2) {
//direction 2 = right to left swipe.
}
}
Useful tip 有用的提示
Alternatively (doesn't look like Ionic have covered this in their gestures doc), you can use HammerJS events in the HTML tag to target a specific swipe direction. 或者(看起来像Ionic已经在他们的手势文档中覆盖了这个),您可以在HTML标记中使用HammerJS事件来定位特定的滑动方向。
<ion-content (swipeleft)="swipeLeftEvent($event)">
Only found this out by trial and error and it seemed to work for most events! 只是通过反复试验找到了它,它似乎适用于大多数事件!
html HTML
<ion-navbar *navbar>
<ion-title>Main</ion-title>
</ion-navbar>
<ion-content padding class="main">
// height: 300px; background-color: #000; => visible for test
<div (pan)='swipeEvent($event)'></div>
</ion-content>
typescrip typescrip
export class MainPage {
constructor(public nav: NavController) { }
swipeEvent($e) {
// swipe left $e.direction = 2;
// pan for get fired position
console.log($e.deltaX+", "+$e.deltaY);
}
}
if deltaX > 0 swipe right else swipe left. 如果deltaX> 0向右滑动,则向左滑动。 i like to use pan
instead of swipe
because i want to make a slide image same as Scrollyeah 我喜欢使用pan
而不是swipe
因为我想制作与Scrollyeah相同的幻灯片图像
You cab bind separate event handlers with Ionic, like: 您可以使用Ionic绑定单独的事件处理程序,例如:
<ion-content on-swipe-left="onSwipeLeft()" on-swipe-right="onSwipeRight()">
You may also use on-swipe
like: 您也可以使用on-swipe
如:
<ion-content on-swipe="swiped($event)">
$scope.swiped = function($e) {
var what = '';
switch ($e.gesture.direction) {
case 'up':
what = 'Swiped up';
break;
case 'down':
what = 'Swiped down';
break;
case 'left':
what = 'Swiped left';
break;
case 'right':
what = 'Swiped right';
break;
default:
what = 'Swiped ???';
break;
}
alert(what)
}
Using Ionic 2 使用Ionic 2
<ion-content (swipe)="swipeEvent($event)">
swipeEvent($e) {
var what = '';
switch ($e.gesture.direction) {
case 'up':
what = 'Swiped up';
break;
case 'down':
what = 'Swiped down';
break;
case 'left':
what = 'Swiped left';
break;
case 'right':
what = 'Swiped right';
break;
default:
what = 'Swiped ???';
break;
}
alert(what)
}
Just to update - as of Ionic 3.19.0, functionality is as follows: 只是为了更新 - 从Ionic 3.19.0开始,功能如下:
<div (swipe)="swipeEvent($event)" />
and 和
swipeEvent($e) {
if($e.offsetDirection == 4){
// Swiped right, for example:
this.week.prevWeek();
} else if($e.offsetDirection == 2){
// Swiped left, for example:
this.week.nextWeek();
}
}
Also - an even quicker inline solution, if you're only concerned with left and right swipes: 此外 - 如果您只关注左右滑动,则更快的内联解决方案:
<div (swipe)="($event.direction === 4) ? calendar.back() : calendar.forward()" />
For Ionic 4+,对于离子 4+,
See my answer on this post here:在这里查看我对这篇文章的回答:
https://stackoverflow.com/a/70089543/9378740 https://stackoverflow.com/a/70089543/9378740
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.