簡體   English   中英

潤飾和移動圖像返回初始位置后 React Native Panresponder 問題

[英]React Native Panresponder issue after retouching and moving the image return back to initial position

我有一個可拖動的汽車圖像,如果我第一次拖動汽車,它在第二次重新拖動汽車圖像返回到初始位置后工作正常。 我想通過手指觸摸平滑地拖動圖像。 請幫我

import React, { Component } from 'react'; import { StyleSheet, View, Text, PanResponder, Animated, Easing, Dimensions, Image, Button } from 'react-native'; 從'react-native'導入{ToastAndroid};

export default class Viewport extends Component {
  constructor(props) {
    super(props);

    this.state = {
      disableCar: false,
      dropZoneCar: null,
      panCar: new Animated.ValueXY(),
    };
    this.carFunction();
  }

  carFunction = () => {
    this.panResponderCar = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onPanResponderMove: Animated.event([null, {
        dx: this.state.panCar.x,
        dy: this.state.panCar.y
      }]),
      onPanResponderGrant: (evt, gestureState) => {
        console.log(evt)
      },

      onPanResponderRelease: (e, gesture) => {
        // console.log(e)

        if (this.isDropZoneCar(gesture)) {
          ToastAndroid.show('Correct', ToastAndroid.SHORT);
        } else {
          ToastAndroid.show('Wrong', ToastAndroid.SHORT);
        }
      }
    });
  }

  isDropZoneCar(gesture) {
    var dz = this.state.dropZoneCar;
    return gesture.moveY > dz.y && gesture.moveY < dz.y + dz.height;
  }

  setDropZoneCar(event) {
    this.setState({
      dropZoneCar: event.nativeEvent.layout
    });
  }
  setDropZoneBike(event) {
    this.setState({
      dropZoneBike: event.nativeEvent.layout
    });
  }
  render() {

    return (
      <View style={styles.mainContainer}>
        <View style={{ flexDirection: 'row', }}>
          <View style={{ flex: 1 }}>
            <View
              onLayout={this.setDropZoneCar.bind(this)}
              style={[styles.dropZone, { backgroundColor: this.state.disableCar ? 'green' : '#2c3e50' }]}>
              <Text style={styles.text}>Drop a Car</Text>
            </View>
            <View
              onLayout={this.setDropZoneBike.bind(this)}
              style={[styles.dropZone, { backgroundColor: this.state.disableCar ? 'green' : '#2c3e50' }]}>
              <Text style={styles.text}>Drop a Bike</Text>
            </View>
          </View>
          <View style={{ flex: 1 }}>
            {this.draggableCar()}
          </View>
        </View>
      </View>
    );
  }

  draggableCar() {
    return (
      <View style={styles.draggableContainer} >
        <Animated.View
          {...this.panResponderCar.panHandlers}
          style={[this.state.panCar.getLayout()]}>
          <Image
            style={{ position: "absolute", width: 200, height: 100, right: 10, top: 300, }}
            source={require('./assets/carr.png')}
          />
        </Animated.View>
      </View>
    );
  }
}

let CIRCLE_RADIUS = 36;
let Window = Dimensions.get('window');
let styles = StyleSheet.create({
  mainContainer: {
    flex: 1
  },
  dropZone: {
    height: 100,
    backgroundColor: '#2c3e50',
    marginTop: 100
  },
  text: {
    marginTop: 25,
    marginLeft: 5,
    marginRight: 5,
    textAlign: 'center',
    color: '#fff'
  },
  draggableContainer: {
    position: 'absolute',
    top: Window.height / 2 - CIRCLE_RADIUS,
    left: Window.width / 2 - CIRCLE_RADIUS,
  },
});

您正在聆聽手指移動dxdy的增量,因此每當您再次觸摸時,您的平移值都會下降到0 每次觸摸時,您都應該在平移值上設置偏移量以解決此問題。 添加這段代碼:

onPanResponderGrant: (e, gestureState) => {
  this.state.panCar.setOffset({x: this.state.panCar.x._value, y: this.state.panCar.y._value});
  this.state.panCar.setValue({x: 0, y: 0});
}

這會將平移的偏移設置為當前位置,因此在后續觸摸后它不會跳回。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM