简体   繁体   English

如何使 React Native Animated.View 可点击?

[英]How to make React Native Animated.View clickable?

I'm using this react native tinder demo ->https://github.com/brentvatne/react-native-animated-demo-tinder我正在使用这个反应原生火种演示->https://github.com/brentvatne/react-native-animated-demo-tinder

Is it possible to make the cards 'clickable' so it would navigate to another view on click?是否可以使卡片“可点击”,以便在点击时导航到另一个视图? I've tried wrapping the "Touchable" components around the animated view but doing so disables the animations.我尝试将“可触摸”组件包裹在动画视图周围,但这样做会禁用动画。

Any ideas would be highly appreciated, thanks!任何想法将不胜感激,谢谢!

Another method (which worked better for me) is to create AnimatedTouchable component using createAnimatedComponent method of Animated:另一种方法(对我来说效果更好)是使用 Animated 的 createAnimatedComponent 方法创建 AnimatedTouchable 组件:

const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);

<AnimatedTouchable onPress={this.onPress}>
  stuff here
</AnimatedTouchable>

I guess you can use TouchableX inside the Animated.View我猜你可以在Animated.View中使用 TouchableX

import { Animated, TouchableOpacity } from 'react-native'
<Animated.View>
  <TouchableOpacity>
    <View>
      stuff
    <View>
  </TouchableOpacity>
</Animated.View>

Ensure you are using the correct imports (not react-native-gesture-handler) otherwise it might not work on Android.确保您使用的是正确的导入(不是 react-native-gesture-handler),否则它可能无法在 Android 上运行。

my variant.我的变种。 This custom AnimatedPressable can also be used inside Animated.View .这个自定义的AnimatedPressable也可以在Animated.View中使用。

import {Text, Animated, Pressable} from 'react-native'
    
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
    
export default () => {
  return (
     <AnimatedPressable onPress={()=>alert('test onPress')}>
        <View>
           <Text>{'....'}</Text>
        </View>
     </AnimatedPressable>
  )
}

In my case it was not working on Android, when I was using就我而言,当我使用它时,它不适用于 Android

import { TouchableOpacity } from 'react-native-gesture-handler'

Use TouchableOpacity provided by React Native.使用 React Native 提供的TouchableOpacity

import { TouchableOpacity } from 'react-native'
const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);

<Animated.View>
<AnimatedTouchable onPress={() => this.props.navigation.goBack()}>
      <BackButton style={{ height: '100%', width: '100%' }} />
    </AnimatedTouchable>

</Animated.View>

it's too late but in my case i have set the height of Animated.View less than the content inside it为时已晚,但就我而言,我已将 Animated.View 的高度设置为小于其中的内容

so make sure that the Animated.View height must be greater or equal to the content on which TouchableOpacity is placed所以请确保 Animated.View 高度必须大于或等于放置 TouchableOpacity 的内容

What works for me when i tried to make a dismissable bottomsheet by pressing on backdrop当我试图通过按下背景来制作可关闭的底片时,什么对我有用

was this :这是:

const renderBackdrop = () => {
const animatedBackdropOpacity = Animated.interpolate(fall, {
  inputRange: [0, 1],
  outputRange: [0.5, 0],
});

return (
  <Animated.View
    pointerEvents={pointerEvents}
    accessibilityViewIsModal
    accessibilityLiveRegion="polite"
    style={StyleSheet.absoluteFill}>
    <TouchableWithoutFeedback onPress={_closeBs}>
      <Animated.View
        style={[
          styles.backdropContainer,
          {
            opacity: animatedBackdropOpacity,
          },
        ]}
      />
    </TouchableWithoutFeedback>
  </Animated.View>
);

}; };

Credit goes to React Native Paper Modal Component https://github.com/callstack/react-native-paper/blob/master/src/components/Modal.tsx#L184归功于 React Native Paper Modal 组件https://github.com/callstack/react-native-paper/blob/master/src/components/Modal.tsx#L184

What about checking ref value?检查 ref 值呢? In example below I use swipes and simulate <TouchableOpacity /> onPress action by checking translation.dx._value在下面的示例中,我使用滑动并通过检查translation.dx._value来模拟<TouchableOpacity /> onPress 动作

export const CoolComponent = () => {
  const theme = StyleSheet.create({
    blockPosition: {
      transform: [{translateX: translation.x}],
    },
  });

  const translation = useRef(new Animated.ValueXY({x: 0, y: 0})).current;
  const panResponder = useRef(
    PanResponder.create({
      onStartShouldSetPanResponderCapture: () => true,
      onStartShouldSetPanResponder: () => true,
      onMoveShouldSetPanResponder: () => false,
      onMoveShouldSetPanResponderCapture: () => false,
      onPanResponderMove: Animated.event(
        [
          null,
          {
            dx: translation.x,
          },
        ],
        {
          useNativeDriver: false,
        },
      ),
      onPanResponderRelease: checkTranslationCoords,
      onPanResponderTerminate: checkTranslationCoords,
    }),
  ).current; 

  const checkTranslationCoords = () => {
    if (Math.abs(translation.x._value) === 0) {
      // TouchableOpacity action here
    }
  };

  return (
    <Animated.View
      {...panResponder.panHandlers}
      style={[theme.blockPosition]}
    >
      <Text>Some text</Text>
      </Animated.View>
  )
}

you need to write a method to animate inside the .您需要编写一个方法来在 .

renderButton: function() {
  return (
    <TouchableOpacity onPress={this._onPressButton}>
      <Image
        style={styles.button}
        source={require('./myButton.png')}
      />
    </TouchableOpacity>
  );
},

in top of the render () you need to write the animated style with in this method在 render() 顶部,您需要在此方法中编写动画样式

_onPressButton(){
........
........
}

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

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