1

据我用谷歌搜索,用两根手指缩放被称为捏合手势(如果我错了,请纠正我)。另外,根据这个问题,我必须PinchGestureHandlerreact-native-gesture-handler库中使用才能检测到这个手势。同样,正如其中一个答案所建议的那样,我需要使用:

<PinchGestureHandler
    onGestureEvent={this.onPinchGestureEvent}
  >
    <View>
          <Camera
              type={cameraType}
              flashMode={flashMode}
              //I need to set my zoom here
              zoom={zoom}
              style={styles.preview}
              ref={camera => this.camera = camera}
          />
    </View>
        </PinchGestureHandler>

onPinchGestureEvent = event => {
      console.log(event.nativeEvent.scale);
      //somehow I need to set my zoom here
    }

当我console.log(event.nativeEvent)从我的onPinchGestureEvent方法中得到以下输出时:

onPinchGestureEvent event:  Object {
  "focalX": 216.5,
  "focalY": 212.5,
  "handlerTag": 8,
  "numberOfPointers": 2,
  "scale": 0.6020446981019169,
  "state": 4,
  "velocity": -0.0031890643353418226,
}

我尝试根据比例设置缩放:

const onPinchGestureEvent = (event) => {
    if (event.nativeEvent.scale >= 1) {
      setZoom(1);
    } else if (event.nativeEvent.scale <= 0) {
      setZoom(0);
    } else {
      setZoom(event.nativeEvent.scale);
    }
  };

但它没有给我顺利的结果,我也不能缩小。也有这个问题本质上是一样的,但是没有人回答。根据this blog,我需要以下内容:

calculateZoom(touch0x, touch0y, touch1x, touch1y) {
    const x = touch0x - touch1x;
    const y = touch0y - touch1y;

    const distance = Math.pow(x, 2) + Math.pow(y, 2);
    
    return Math.sqrt(distance);
  }

但从我的角度来看,我console.log(event.nativeEvent)只有一个focalX和一个focalY值。

任何建议、博客、想法都将被授予!

4

0 回答 0