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

Den*_*nny 10 javascript animation ios react-native

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

是否可以使卡片"可点击",以便在点击时导航到另一个视图?我已经尝试在动画视图周围包装"可触摸"组件,但这样做会禁用动画.

任何想法都将受到高度赞赏,谢谢!

Ole*_*leg 24

另一种方法(对我来说效果更好)是使用Animated的createAnimatedComponent方法创建AnimatedTouchable组件:

const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);

<AnimatedTouchable onPress={this.onPress}>
  stuff here
</AnimatedTouchable>
Run Code Online (Sandbox Code Playgroud)

  • 实际上这应该是选定的答案,因为包装在`Animated.View`中可能会弄乱你的样式并且需要不必要的`View` (2认同)

Nis*_*kar 15

我想你可以在Animated.View中使用TouchableX

<Animated.View>
  <TouchableOpacity>
    <View>
      stuff
    <View>
  </TouchableOpacity>
</Animated.View>`
Run Code Online (Sandbox Code Playgroud)

  • 我做到了,但是它禁用了动画。@Denny知道了它的解决方案吗? (2认同)
  • 额外的 &lt;View&gt; 在这里是多余的,因为 TouchableOpacity 本身是一个视图并且理解所有视图的道具 (2认同)

Dmy*_*nko 5

我的变种。这个自定义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>
  )
}
Run Code Online (Sandbox Code Playgroud)