cheeseC
cheeseC
2017-06-09 06:11

hammer.js 的pinch事件

10
  • hammer.js
 这里的pinch事件 alert(evt.type)可以alert出来,但是 进不到 onUIMessage(evt) 方法的 if (evt.type == 'pinchstart' || evt.type == 'pinchend' || evt.type == 'pinchmove') {}里边,但是却能进else if (evt.type == 'pan' || evt.type == 'panstart' || evt.type == 'panend' || evt.type == 'panmove') {} 里,有没有大神指导怎么回事啊


// 触摸按下
thisHammer.on("panstart panmove panend", function (evt) {
    onUIMessage(evt);
    console.log(evt.type);
});
//Pinch
thisHammer.add(new Hammer.Pinch());
thisHammer.on("pinchstart pinchmove pinchend", function (evt) {
    alert(evt.type);
   onUIMessage(evt);
  });
/*
* UI Message
*/
function onUIMessage(evt) {
    evt.preventDefault(); //通知浏览器不要执行与事件关联的默认动作。

    var msgType = this.getMsgType(evt); // 事件消息类型
    if (msgType == "") {
        return;
    }

    var eventMsg = { name: msgType };
   /* eventMsg.pageX = evt.pageX;
    eventMsg.pageY = evt.pageY;
    eventMsg.clientX = evt.clientX;
    eventMsg.clientY = evt.clientY;
    eventMsg.ctrlKey = evt.ctrlKey;
    eventMsg.altKey = evt.altKey;
    eventMsg.shiftKey = evt.shiftKey;
    eventMsg.scale = evt.scale;*/
    if (evt.type == 'mousewheel') {
        var delta = (evt.originalEvent.wheelDelta && (evt.originalEvent.wheelDelta > 0 ? 1 : -1));
        if (delta > 0) {
            // 向上滚
            console.log("wheelup");
        } else if (delta < 0) {
            // 向下滚
            console.log("wheeldown");
        }
        eventMsg.wheelDelta = delta;
        console.log(eventMsg.wheelDelta );

    }

    if (evt.type == 'keydown') {
        eventMsg.keyCode = evt.keyCode;
        console.log(eventMsg.keyCode);
    }
    alert('out');
    eventMsg.annoName = this._currAnnoName;
    if (evt.type == 'pinchstart' || evt.type == 'pinchend' || evt.type == 'pinchmove') {
        alert('come in');
        if (evt.pointers.length == 2) {
            var offsetX = evt.pointers[1].clientX - evt.pointers[0].clientX;
            var offsetY = evt.pointers[1].clientY - evt.pointers[0].clientY;
            eventMsg.twoFingerDistance = Math.sqrt(offsetX * offsetX + offsetY * offsetY);
            eventMsg.clientX = evt.center.x;
            eventMsg.clientY = evt.center.y;
            eventMsg.x = evt.center.x;
            eventMsg.y = evt.center.y;
            alert(eventMsg.name+ offsetX +offsetY+eventMsg.twoFingerDistance+eventMsg.x +eventMsg.clientX );
        }
        eventMsg.annoName = "imageZoom";
    }
    else if (evt.type == 'pan' || evt.type == 'panstart' || evt.type == 'panend' || evt.type == 'panmove') {
        if (evt.srcEvent != null && evt.srcEvent != undefined) {
            if (evt.pointerType == "mouse") {
                eventMsg.x = evt.srcEvent.x;
                eventMsg.y = evt.srcEvent.y;
                eventMsg.pageX = evt.srcEvent.pageX;
                eventMsg.pageY = evt.srcEvent.pageY;
                eventMsg.clientX = evt.srcEvent.clientX;
                eventMsg.clientY = evt.srcEvent.clientY;
            }
            else {
                if (evt.srcEvent.changedTouches != undefined) {
                    eventMsg.x = evt.srcEvent.changedTouches[0].screenX;
                    eventMsg.y = evt.srcEvent.changedTouches[0].screenY;
                    eventMsg.pageX = evt.srcEvent.changedTouches[0].pageX;
                    eventMsg.pageY = evt.srcEvent.changedTouches[0].pageY;
                    eventMsg.clientX = evt.srcEvent.changedTouches[0].clientX;
                    eventMsg.clientY = evt.srcEvent.changedTouches[0].clientY;
                }
                eventMsg.ctrlKey = evt.srcEvent.ctrlKey;
                eventMsg.altKey = evt.srcEvent.altKey;
                eventMsg.shiftKey = evt.srcEvent.shiftKey;
                alert(eventMsg.name+eventMsg.x+eventMsg.y+eventMsg.pageX+eventMsg.pageY +eventMsg.clientX );
            }
        }
    }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换