Simplest way to detect a pinch

前端 未结 7 2074
清酒与你
清酒与你 2020-11-28 19:29

This is a WEB APP not a native app. Please no Objective-C NS commands.

So I need to detect \'pinch\' events on iOS. Problem is every plugin or metho

7条回答
  •  感动是毒
    2020-11-28 20:06

    detect two fingers pinch zoom on any element, easy and w/o hassle with 3rd party libs like Hammer.js (beware, hammer has issues with scrolling!)

    function onScale(el, callback) {
        let hypo = undefined;
    
        el.addEventListener('touchmove', function(event) {
            if (event.targetTouches.length === 2) {
                let hypo1 = Math.hypot((event.targetTouches[0].pageX - event.targetTouches[1].pageX),
                    (event.targetTouches[0].pageY - event.targetTouches[1].pageY));
                if (hypo === undefined) {
                    hypo = hypo1;
                }
                callback(hypo1/hypo);
            }
        }, false);
    
    
        el.addEventListener('touchend', function(event) {
            hypo = undefined;
        }, false);
    }
    

提交回复
热议问题