【问题标题】:How to get rotation angle in ionic gesture API?如何在离子手势 API 中获取旋转角度?
【发布时间】:2020-11-18 08:48:59
【问题描述】:

我正在画布上工作,并且正在使用hammerjs 处理手势事件。但是我遇到了一个问题,我已经问过here 和其他地方但没有人回应。所以现在我正在使用离子手势,但我也找不到旋转角度属性。有人可以帮我吗?

代码如下:

ngAfterViewInit() {
      const gesture: Gesture = this.gestureCtrl.create({
      el: this.transCanvasElement,
      gestureName: 'rotate',
      onStart: ev => this.onStartHandler(ev)
    }, true);
    gesture.enable();
}
onStartHandler(ev) {
    console.log('ionic gesture ev angle: ', ev.rotation); // ev.rotation is undefined
}

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    this 在 JS 中的含义取决于作用域。有关更多信息,请参阅here。您可以将this 绑定到回调或使其成为箭头函数。

    试试下面的

    选项1:绑定this

    ngAfterViewInit() {
      const gesture: Gesture = this.gestureCtrl.create({
        el: this.transCanvasElement,
        gestureName: 'rotate',
        onStart: this.onStartHandler.bind(this)  // <-- bind `this` to the callback
      }, true);
      gesture.enable();
    }
    
    onStartHandler(ev) {
      console.log('ionic gesture ev angle: ', ev.rotation);
    }
    

    选项2:箭头函数

    ngAfterViewInit() {
      const gesture: Gesture = this.gestureCtrl.create({
        el: this.transCanvasElement,
        gestureName: 'rotate',
        onStart: ev => this.onStartHandler(ev)
      }, true);
      gesture.enable();
    }
    
    onStartHandler = (ev) => {                           // <-- arrow function
      console.log('ionic gesture ev angle: ', ev.rotation);
    }
    

    【讨论】:

    • 选项 1 给出错误 bind() 方法在 void 类型上不存在,选项 2 给出与以前相同的结果。 ev.rotation 仍未定义。
    • 是的,选项中不需要箭头符号。我已经更新了答案。关于选项 2 中的错误,请检查 rotation 属性是否在 ev 变量中实际可用。尝试仅打印ev 进行验证。
    • 不,没有旋转属性。这就是为什么我问这个问题是否有人可以帮助我在 ev 的嵌套对象中找到它。手势 API 中没有旋转属性很奇怪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    相关资源
    最近更新 更多