简体   繁体   English

React Native On Press 未按预期工作

[英]React Native On Press Not Working As Expected

I'm trying to put an image on that press location when I click somewhere, but I'm running into an error.当我单击某处时,我试图在该新闻位置放置图像,但我遇到了错误。 When I click an already existing sticker, it gets the coordinates of the on press relative to the sticker, so if I clicked on the top left of my already existing sticker, it would just be in the top left of the screen.当我点击一个已经存在的贴纸时,它会获得相对于贴纸的 on press 坐标,所以如果我点击我已经存在的贴纸的左上角,它只会在屏幕的左上角。 why is this happening?为什么会这样? Here is my code这是我的代码

<TouchableOpacity
    activeOpacity={1}
    onPress={(event: GestureResponderEvent) => {
    const { locationX: x, locationY: y } = event.nativeEvent;
    setStickers(stickers.concat({ x, y }));
    console.log("X: " + x);
    console.log("Y: " + y);
    }}
    style={styles.imageButton}>

          <Image style={styles.fullImage} source={{ uri }} />
          {stickers.map((sticker, index) => {
            return (
              <Image
                source={stickerImage}
                style={{
                  width: 100,
                  height: 100,
                  position: "absolute",
                  top: sticker.y,
                  left: sticker.x,
                }}
                key={index}
              />
            );
          })}
</TouchableOpacity>

My solution was to use pageX and pageY instead of locationX and locationY.我的解决方案是使用 pageX 和 pageY 而不是 locationX 和 locationY。 Thank you to the person who helped me.感谢帮助我的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM