在第二个输入上反应原生动画PanResponder反向

时间:2018-05-24 14:30:06

标签: javascript reactjs animation react-native draggable

我使用PanResponder与React Native API的Animated结合使用了一个组件。我的组件的代码:

import React, { Component } from 'react';
import { Animated, PanResponder } from 'react-native';

import { SVG } from '../';
import { Icon, LockContainer, StatusCircle } from './styled';

class VehicleLock extends Component {
  state = {
    pan: new Animated.ValueXY({ x: 9, y: 16 }),
  };

  componentWillMount() {
    this.animatedValueY = 0;

    this.minYValue = 16;
    this.maxYValue = 175;

    this.state.pan.y.addListener((value) => {
      this.animatedValueY = value.value;
    });

    this.panResponder = PanResponder.create({
      onStartShouldSetPanResponderCapture: () => true,
      onMoveShouldSetPanResponderCapture: () => true,

      onPanResponderGrant: (evt, gestureState) => {
        this.state.pan.setOffset({ x: 0, y: 0 });
        // this.state.pan.setOffset(this.state.pan.__getValue());
        this.state.pan.setValue({ x: 9, y: this.minYValue });
      },
      onPanResponderMove: (evt, gestureState) => {
        // deltaY: amount of pixels moved vertically since the beginning of the gesture
        let newY = gestureState.dy;

        if (newY < this.minYValue) {
          newY = this.minYValue;
        } else if (newY > this.maxYValue) {
          newY = this.maxYValue;
        }

        Animated.event([null, {
          dy: this.state.pan.y,
        }])(evt, {
          dy: newY,
        });
      },
      onPanResponderRelease: (evt, gestureState) => {
        let newY = this.minYValue;
        const releaseY = gestureState.dy;

        if (releaseY > 83) {
          newY = this.maxYValue;
        }

        Animated.spring(this.state.pan, {
          toValue: {
            x: 9,
            y: newY,
          },
        }).start();
      },
    });
  }

  componentWillUnmount() {
    this.state.pan.x.removeAllListeners();
    this.state.pan.y.removeAllListeners();
  }

  render() {
    const customStyles = {
      ...this.state.pan.getLayout(),
      position: 'absolute',
      zIndex: 10,
      transform: [
        {
          rotate: this.state.pan.y.interpolate({
            inputRange: [this.minYValue, this.maxYValue],
            outputRange: ['0deg', '180deg'],
          }),
        },
      ],
    };

    return (
      <LockContainer>
        <SVG icon="lock_open" width={16} height={21} />

        <Animated.View
          {...this.panResponder.panHandlers}
          style={customStyles}
        >
          <StatusCircle>
            <Icon>
              <SVG icon="arrow_down" width={23} height={23} />
            </Icon>
          </StatusCircle>
        </Animated.View>

        <SVG icon="lock_closed" width={16} height={21} />
      </LockContainer>
    );
  }
}

export default VehicleLock;

正如您在我的代码中所看到的,我使用边界为Y值设置动画。它必须保持在某些值之间的框中。一旦用户释放拖动并且它超过最大Y值的一半,它就会动画到最大值。

这没有任何问题,但在第二次互动中我想要扭转行动。因此,它不应该下降,而是必须上升。不幸的是,在发布时,Y值会重置。

正如您在我的代码中的注释中所看到的,我知道移动基于delta,因此自交互开始以来移动的Y值。这篇精彩的评论PanResponder snaps Animated.View back to original position on second drag对此进行了解释。

虽然我不知道如何解决它。在这里你可以看到我目前的行为: enter image description here

在第二个输入上,元素快速回到顶部。这是预期的行为。正如@jevakallio在评论中所述,您可以重置onPanResponderGrant中偏移量中的值。当我这样做(注释掉)时,元素会重置值,但在第二次交互时,它会为容器中的外部设置动画。因此,在这种情况下,0maxYValue,它会将容器外的175设置为底部。

如何将外部反转动画制作回顶部?我似乎没有得到这个。提前致谢!

1 个答案:

答案 0 :(得分:1)

根据Panresponder docsonPanResponderGrant应指示手势的开始。因此,如果此时设置this.state.pan.setOffsetthis.state.pan.setValue,它将在每个手势的开头重置。尝试onPanResponderGrant中的console.log,看看会发生什么。

此外,实现PanResponder的父级或包装组件可能会干扰您的PanResponder。这可能是解决问题的另一个好起点。

来自PanResponder文档:

onPanResponderGrant: (evt, gestureState) => {
    // The gesture has started. Show visual feedback so the user knows
    // what is happening!

    // gestureState.d{x,y} will be set to zero now
  },
相关问题