【问题标题】:Implement pinch zoom using one touch react native使用一键式 React Native 实现捏缩放
【发布时间】:2021-09-04 21:00:52
【问题描述】:

我想在 react native 中使用单指触摸来实现捏合手势(缩放)。 ![图像侧面有四个箭头。需要使用单点触摸实现捏合缩放][1]

https://i.stack.imgur.com/ykqSb.png

【问题讨论】:

    标签: ios reactjs react-native react-redux react-native-gesture-handler


    【解决方案1】:

    您可以使用 react-native-gesture-handler 来实现这一点。我只是写了一些代码。首先长按角会显示要拖动的指示器,然后拖动会改变图像的比例。我没有完成所有功能,只是向您展示一些逻辑。也许你应该自己完成!

    https://snack.expo.io/@gwl002/onetouchzoom

    同样代码的世博小吃

    
    import { StatusBar } from 'expo-status-bar';
    import React, { useState, useRef } from 'react';
    import { StyleSheet, Text, View, Image, Alert, Animated } from 'react-native';
    import 'react-native-gesture-handler';
    import { TapGestureHandler, PanGestureHandler, LongPressGestureHandler, State } from 'react-native-gesture-handler';
    
    export default function App() {
      const [dirButton, setDirButton] = useState(null);
      const imgLongPress = useRef();
      const imgPan = useRef();
      const scaleAnim = new Animated.Value(1);
    
      return (
        <View style={styles.container}>
          <StatusBar style="auto" />
          <LongPressGestureHandler
            ref={imgLongPress}
            simultaneousHandlers={imgPan}
            onHandlerStateChange={({ nativeEvent }) => {
              if (nativeEvent.state === State.ACTIVE) {
                let { x, y } = nativeEvent;
                let buttonPosition = "";
                console.log(x, y);
                if (x < 30 && y < 30) {
                  buttonPosition = "topLeft";
                } else if (x > 370 && y < 30) {
                  buttonPosition = "topRight";
                } else if (x < 30 && y > 370) {
                  buttonPosition = "bottomLeft";
                } else if (x > 370 && y > 370) {
                  buttonPosition = "bottomRight";
                } else {
                  buttonPosition = null
                }
                setDirButton(buttonPosition)
              }
            }}
            minDurationMs={800}
          >
            <PanGestureHandler
              ref={imgPan}
              simultaneousHandlers={imgLongPress}
              onGestureEvent={({ nativeEvent }) => {
                if (dirButton) {
                  console.log(nativeEvent)
                  let { x, y } = nativeEvent;
                  if (dirButton === "topRight" || dirButton === "bottomRight") {
                    scaleAnim.setValue(x / 400)
                  } else if (dirButton === "topLeft" || dirButton === "bottomLeft") {
                    scaleAnim.setValue((400 - x) / 400)
                  }
                }
              }}
            >
              <View style={styles.wrapper}>
                <Animated.Image
                  source={require("./assets/test.png")}
                  style={[
                    { width: 400, height: 400 },
                    {
                      transform: [
                        { scale: scaleAnim }
                      ]
                    }
                  ]}
                />
                <View
                  style={[styles.indicator, dirButton ? styles[dirButton] : { width: 0, height: 0 }]}
                >
    
                </View>
              </View>
            </PanGestureHandler>
          </LongPressGestureHandler>
    
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        paddingTop: 100,
        backgroundColor: '#fff',
      },
      wrapper: {
        position: "relative",
      },
      indicator: {
        width: 30,
        height: 30,
        backgroundColor: "blue",
        position: "absolute"
      },
      topLeft: {
        left: 0,
        top: 0
      },
      topRight: {
        right: 0,
        top: 0
      },
      bottomLeft: {
        left: 0,
        bottom: 0,
      },
      bottomRight: {
        right: 0,
        bottom: 0
      }
    });
    
    

    【讨论】:

    • 可以运行演示但不能进行缩放。你能帮帮我吗
    • 试试长按左上角,会不会出现蓝色指示灯?
    • 但是我也面临一个同样的问题,即每当您第一次尝试缩放时,它会首先放大/缩小导致问题
    • 也许你应该粘贴你的代码。很难知道你真正的问题。
    猜你喜欢
    • 2015-10-16
    • 2018-02-07
    • 2012-10-04
    • 2022-10-18
    • 1970-01-01
    • 2021-11-18
    • 2013-11-17
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多