【问题标题】:How do I create a flippable card in react-native?如何在 react-native 中创建可翻转卡片?
【发布时间】:2022-01-13 00:58:42
【问题描述】:

具体来说,如何让卡片的一侧在翻转后消失。我使用的是 Android,所以 backfaceVisibility 没有帮助解决我的问题。我使用动画通过不透明度移除对象,但问题是一旦我移除卡片的一侧,“不可见”按钮仍然有效,而卡片当前一侧的按钮则没有。我尝试在 Pressable 中使用 zIndex 和“禁用”,但我不确定如何解决这个问题。我在下面附上了源代码:

  let animatedValue = new Animated.Value(0)
  let val = 0;
  animatedValue.addListener(({ value }) => {
    val = value;
  })
  let frontOpacity = animatedValue.interpolate({ 
    inputRange: [89, 90], 
    outputRange: [1, 0] 
  })
  let backOpacity = animatedValue.interpolate({ 
    inputRange: [89, 90], 
    outputRange: [0, 1] 
  })
  let frontInterpolate = animatedValue.interpolate({
    inputRange: [0, 180],
    outputRange: ['0deg', '180deg']
  })
  let backInterpolate = animatedValue.interpolate({
    inputRange: [0, 180],
    outputRange: ['180deg', '360deg']
  })

  const frontAnimatedStyle = {
    transform: [
      { rotateY: frontInterpolate }
    ], 
    opacity: frontOpacity,
  }
  const backAnimatedStyle = {
    transform: [
      { rotateY: backInterpolate }
    ], 
    opacity: backOpacity,
  }

  let isFront = true;
  const flipCard = () => {
    isFront = !isFront;
    if (val >= 90) {
      Animated.spring(animatedValue, {
        toValue:0, 
        friction: 8,
        tension: 10,
        useNativeDriver: true,
      }).start();
    } else {
    Animated.spring(animatedValue, {
      toValue:180, 
      friction: 8,
      tension: 10,
      useNativeDriver: true,
    }).start();
    }
  }

  const getFrontZ = () => {
    return isFront ? 1 : 0;
  }

  return(
    <View style={styles.container}>
      <Animated.View 
        style={[styles.cardStyle, 
                styles.frontCardStyle, 
                frontAnimatedStyle, 
                {height: cardHeight, zIndex: -1,}]}>
        <Pressable 
          onPress={() => console.log('Open Stack')}
          style={{height: '100%', width: '100%'}}>
          <Pressable 
            onPress={() => flipCard()}
            style={{backgroundColor: 'blue', height: 50, width: 50,}}
          >
          </Pressable>
        </Pressable>
      </Animated.View>
      <Animated.View 
      style={[styles.cardStyle, 
              styles.backCardStyle, 
              backAnimatedStyle, 
              {height: cardHeight, zIndex: 0}]}>
        <Pressable 
          onPress={() => flipCard()}
          style={{height: '100%', width: '100%'}}
          >
        </Pressable>
      </Animated.View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center',
  },
  cardStyle: {
    height: '100%',
    width: '100%',
    backfaceVisibility: 'hidden',
  },
  frontCardStyle: {
    backgroundColor: 'red',
  },
  backCardStyle: {
    backgroundColor: 'purple',
    position: 'absolute',
    top: 0,
  }
})

【问题讨论】:

    标签: javascript node.js react-native flip


    【解决方案1】:

    试试这个库react-native-card-flip

    并将其导入您的文件并像这样使用 import CardFlip from 'react-native-card-flip';

    <CardFlip style={styles.cardContainer} ref={(card) => this.card = card} >
        <TouchableOpacity style={styles.card} onPress={() => this.card.flip()} > 
            <Text>AB</Text></TouchableOpacity>
        <TouchableOpacity style={styles.card} onPress={() => this.card.flip()} > 
        <Text>CD</Text></TouchableOpacity>
    </CardFlip>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      相关资源
      最近更新 更多