我是Angular 2的新手,正在寻找一种为移动用户实现良好的Tab Touch Swipe Navigation,其滑动过渡到下一个TAB视图。
到目前为止有人知道为Angular 2实现基于选项卡的导航的好方法吗?任何反馈都将不胜感激。:)
用于滑动检测是一个简单的解决方案:
:在app.component.html中:
<div (touchstart)="swipe($event, 'start')" (touchend)="swipe($event, 'end')">
App content here
</div>
在app.component.ts中:
private swipeCoord?: [number, number];
private swipeTime?: number;
swipe(e: TouchEvent, when: string): void {
const coord: [number, number] = [e.changedTouches[0].clientX, e.changedTouches[0].clientY];
const time = new Date().getTime();
if (when === 'start') {
this.swipeCoord = coord;
this.swipeTime = time;
} else if (when === 'end') {
const direction = [coord[0] - this.swipeCoord[0], coord[1] - this.swipeCoord[1]];
const duration = time - this.swipeTime;
if (duration < 1000 //
&& Math.abs(direction[0]) > 30 // Long enough
&& Math.abs(direction[0]) > Math.abs(direction[1] * 3)) { // Horizontal enough
const swipe = direction[0] < 0 ? 'next' : 'previous';
// Do whatever you want with swipe
}
}
}
注意:我尝试了Hammerjs解决方案,但是将其配置为忽略鼠标手势是不可能的,因为您无法直接访问锤子对象。因此,选择一些文本将导航到下一页...
您可以使用HammerJS
实现触摸操作,例如,您可以按照此plunker进行操作。
包括hammer.js文件
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.js"></script>
或
npm install hammerjs --save
用于使用Hammerjs的浏览器触摸支持,包括
<script src="http://cdn.rawgit.com/hammerjs/touchemulator/master/touch-emulator.js"></script>
<script>
在app.module.ts
中导入import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
export class MyHammerConfig extends HammerGestureConfig {
overrides = <any>{
'swipe': {velocity: 0.4, threshold: 20} // override default settings
}
}
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [{
provide: HAMMER_GESTURE_CONFIG,
useClass: MyHammerConfig
}] // use our custom hammerjs config
})
plunker链接例如
实现选项卡angular2-material
是一个很好的起点,请遵循此链接
我从@elvis metodiev答案和@pikiou创建了一个指令:
Swipe.Directive.ts
import { Directive, EventEmitter, HostListener, Output } from '@angular/core';
@Directive({ selector: '[swipe]' })
export class SwipeDirective {
@Output() next = new EventEmitter<void>();
@Output() previous = new EventEmitter<void>();
swipeCoord = [0, 0];
swipeTime = new Date().getTime();
constructor() { }
@HostListener('touchstart', ['$event']) onSwipeStart($event) {
this.onSwipe($event, 'start');
}
@HostListener('touchend', ['$event']) onSwipeEnd($event) {
this.onSwipe($event, 'end');
}
onSwipe(e: TouchEvent, when: string) {
this.swipe(e, when);
}
swipe(e: TouchEvent, when: string): void {
const coord: [number, number] = [e.changedTouches[0].clientX, e.changedTouches[0].clientY];
const time = new Date().getTime();
if (when === 'start') {
this.swipeCoord = coord;
this.swipeTime = time;
} else if (when === 'end') {
const direction = [coord[0] - this.swipeCoord[0], coord[1] - this.swipeCoord[1]];
const duration = time - this.swipeTime;
if (duration < 1000 //
&& Math.abs(direction[0]) > 30 // Long enough
&& Math.abs(direction[0]) > Math.abs(direction[1] * 3)) { // Horizontal enough
const swipeDir = direction[0] < 0 ? 'next' : 'previous';
if (swipeDir === 'next') {
this.next.emit();
} else {
this.previous.emit();
}
}
}
}
}
tour.component.component.ts
<div
...
swipe
(next)="onRotateNext()"
(previous)="onRotatePrevious()"
>
...
</div>
首先安装Hammerjs和Action Touch-Action Polyfill:
$ npm install hammerjs hammer-timejs
然后将导入添加到'app.module.ts'中,以便使用/捆绑:
import 'hammerjs';
import 'hammer-timejs';
现在您可以处理动作的事件:
旋转
捏
按
pan
点击
滑动
例如,您可以说:
<li *ngFor="let employee of employeesList;" (swiperight)="myswiperight(employee)" (swipeleft)="myswipeleft(employee)">
或:
<div (panstart)="onPanStart($event)" (panmove)="onPan($event)">
参考:https://saschwarz.github.io/angular2-gestures-slides/#/
我设法提出了一种写入的函数,我将其放在一个名为"手势"的dir中。然后创建一个名为" Swipe.ts"的文件。并将其放在里面。
let swipeCoord = [0, 0];
let swipeTime = new Date().getTime();
export function swipe(e: TouchEvent, when: string): void {
const coord: [number, number] = [e.changedTouches[0].clientX, e.changedTouches[0].clientY];
const time = new Date().getTime();
if (when === 'start') {
swipeCoord = coord;
swipeTime = time;
} else if (when === 'end') {
const direction = [coord[0] - swipeCoord[0], coord[1] - swipeCoord[1]];
const duration = time - swipeTime;
if (duration < 1000 //
&& Math.abs(direction[0]) > 30 // Long enough
&& Math.abs(direction[0]) > Math.abs(direction[1] * 3)) { // Horizontal enough
const swipeDir = direction[0] < 0 ? 'next' : 'previous';
if (swipeDir === 'next') {
alert('swipe next');
} else {
alert('swipe prev');
}
}
}
}
然后导入所需的组件,例如:
import {swipe} from '../../gestures/swipe';
并创建一个称为:
的函数onSwipe(e: TouchEvent, when: string) {
swipe(e, when);
}
在所需组件的HTML中,请使用:
<div (touchstart)="onSwipe($event, 'start')"
(touchend)="onSwipe($event, 'end')">
<!-- whatever content you have goes here -->
</div>
ps-信用转到@pikiou。我只是想出了更高的抽象水平,这对我来说更有意义。