【问题标题】:How to animate height to 100% in react-native?如何在 react-native 中将高度设置为 100%?
【发布时间】:2020-02-15 17:46:05
【问题描述】:

我一直在努力:

        <Animated.View
          style={[{
            height: this.collapse.interpolate({
              inputRange: [0, 1],
              outputRange: ['0%', '100%'],
            }),
          }]}
        >
          {children}
        </Animated.View>

但列表中的一项采用FlatList 的总高度。

我尝试使用minHeight,但同样的问题。

如何将动画高度设为 100%?

复制

小吃:https://snack.expo.io/@kopax/petrified-cookies

在浏览器中,高度不是&lt;FlatList /&gt;的高度,但在本机上,它看起来像这样:

如何为这个动画使用动态高度?

【问题讨论】:

  • 你能分享到snack吗?
  • 我刚刚用小吃复制品更新了我的答案。
  • 请注意,您可以在动画视图的父级上设置childrenContainerStylecontainerStyled,请参阅:github.com/software-mansion/react-native-gesture-handler/blob/…。我还发现 maxHeightminHeight 在 react native 中不能很好地配合使用。到目前为止,我已经尝试过使用可用的 css,例如 flexminHeightmaxHeightheight,但还没有找到一种实现动态高度的方法。
  • @OliverD 有问题吗?
  • 很遗憾,不,检查@needsleep 答案:)

标签: javascript react-native expo react-animated react-native-web


【解决方案1】:

如果您的项目具有不同的高度,那么一种解决方案是在呈现可滑动项目时使用 onLayout 函数计算此高度,然后将此值用于您的动画。

class GmailStyleSwipeableRow extends Component {
  static animationDeleteDuration = 200; // eslint-disable-line react/sort-comp

  constructor(props) {
    super(props);
    this.collapse = new Animated.Value(1);
    this.height = 75;
  }

  render () {
  return (<Swipeable>
    <Animated.View
        onLayout={event => {
            const { height } = event.nativeEvent.layout;
            this.height = height;
        }}
        style={[
            {
            minHeight: 75, //give it a default minHeight
            height: this.collapse.interpolate({ 
                inputRange: [0, 1],
                outputRange: [0, this.height], //base your animation on the calculated height
            }),
            },
        ]}
        >
      {children}
    </Animated.View>
  </Swipeable>)
  }
}

但是,如果您的项目与您的示例类似,那么您可以保存所有这些计算,并为项目提供相同的高度。

【讨论】:

  • 它们的高度不同,所以我们不能给出相同的高度(我再次确认)。关于onLayout,会触发多次渲染。我仍然尝试使用它,因为它是我的一个选择,它并没有像我预期的那样改变高度。您介意用snack.expo.io 更新您的问题吗?
  • 我已经尝试过你的解释,在构造函数中设置minHeightthis.height = 75 将设置最终高度。我已经删除了它,现在我得到了完整的高度,但是由于某种原因动画没有崩溃,也许 onLayout 正在多次重置值?
  • interpolate 中 this.height 的值始终未定义,我还没有找到可靠的方法从 onLayout 设置值并在样式中使用它。
  • 对此有任何解决方案吗??
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多