为什么我在iOS 17+上使用react-native-linear-gradient时会崩溃

vql8enpb  于 4个月前  发布在  iOS
关注(0)|答案(2)|浏览(107)

在过去的几周里,我注意到AppStoreConnect中出现了大量与LinearGradient相关的新崩溃,这些崩溃是我从react-native-linear-gradient导入的
这些崩溃日志只有iOS 17+,所以我猜这与升级中发生的变化有关,但我真的不太关心这个变化,我只想知道如何在我的应用程序中修复它并防止崩溃。
这是我的组件的简化可复制版本,一个从左到右动画的栏,在iOS 16及以下运行良好,但在17+上崩溃,终端中没有日志

import React, { useState, useEffect } from 'react';
import { View, Animated } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';

const AnimatedGradient = ({ credit = 0 }) => {
  const [barWidth, setBarWidth] = useState(1);
  const position = new Animated.Value(0);

  useEffect(() => {
    Animated.timing(position, {
      toValue: credit,
      duration: 1000,
      useNativeDriver: false,
    }).start();
  }, [credit]);

  const animatedWidth = position.interpolate({
    inputRange: [0, 1],
    outputRange: [0, barWidth],
  });

  return (
    <View onLayout={(event) => setBarWidth(event.nativeEvent.layout.width)}>
      <Animated.View style={{ width: animatedWidth }}>
        <LinearGradient
          colors={['blue', 'darkblue']}
          start={{ x: 0, y: 0 }}
          end={{ x: 1, y: 0 }}
          style={{ height: 10 }}
        />
      </Animated.View>
    </View>
  );
};

export default AnimatedGradient;

字符串

628mspwn

628mspwn1#

你必须更新react-native-linear-gradient到v2.8.3来避免这个崩溃,这个崩溃属于NSInternalInconsistencyException
查看更多:https://github.com/react-native-linear-gradient/react-native-linear-gradient/issues/637#issuecomment-1725219069

yxyvkwin

yxyvkwin2#

经过一些痛苦的调试,我发现在所有iOS 17+版本上,创建一个宽度为0的react-native-linear-gradient会导致应用程序硬崩溃。这是一个应该尽快提交到他们的软件包中的错误。
上面代码中的这一更改修复了这个问题-注意,范围现在从1到barWidth,而不是从0开始

const animatedWidth = position.interpolate({
    inputRange: [0, 1],
    outputRange: [1, barWidth],
  });

字符串

相关问题