我需要在Ionic 2应用程序中使用swipeup / swipedown手势。当我做的时候
<div (swipe)='someFunction($event)'></div>
然后我的 someFunction(e)
被调用,但仅限于水平幻灯片 - 因此我无法听到上下滑动方向。 (swipeup)
和 (swipedown)
似乎根本不做任何事情。您是否知道Ionic beta是否可以实现这一点?
答案 0 :(得分:2)
Ionic 2 makes use hammerjs library to handle its gestures.
They’ve also built their own Gesture class that effectively acts as a wrapper to hammerjs: Gesture.ts.
So you can do your own directive like:
import {Directive, ElementRef, Input, OnInit, OnDestroy} from 'angular2/core'
import {Gesture} from 'ionic-angular/gestures/gesture'
declare var Hammer: any
/*
Class for the SwipeVertical directive (attribute (swipe) is only horizontal).
In order to use it you must add swipe-vertical attribute to the component.
The directives for binding functions are [swipeUp] and [swipeDown].
IMPORTANT:
[swipeUp] and [swipeDown] MUST be added in a component which
already has "swipe-vertical".
*/
@Directive({
selector: '[swipe-vertical]' // Attribute selector
})
export class SwipeVertical implements OnInit, OnDestroy {
@Input('swipeUp') actionUp: any;
@Input('swipeDown') actionDown: any;
private el: HTMLElement
private swipeGesture: Gesture
private swipeDownGesture: Gesture
constructor(el: ElementRef) {
this.el = el.nativeElement
}
ngOnInit() {
this.swipeGesture = new Gesture(this.el, {
recognizers: [
[Hammer.Swipe, {direction: Hammer.DIRECTION_VERTICAL}]
]
});
this.swipeGesture.listen()
this.swipeGesture.on('swipeup', e => {
this.actionUp()
})
this.swipeGesture.on('swipedown', e => {
this.actionDown()
})
}
ngOnDestroy() {
this.swipeGesture.destroy()
}
}
This code allows you to do something like this:
<div swipe-vertical [swipeUp]="mySwipeUpAction()" [swipeDown]="mySwipeDownAction()">
答案 1 :(得分:1)
只是更新,Ionic现在有手势控制。见
http://ionicframework.com/docs/v2/components/#gestures
手势会返回$event
个对象。您可以使用此数据来检查它是否是一个swipeup / swipedown事件。