Jim*_*Jim 3 reactjs react-native react-animated
目标:
创建一个下拉视图,折叠时高度为 75 像素。展开后,其高度为 225px。当按下启动动画的按钮时,它会并行运行两个动画,一个用于高度的计时函数,一个用于不透明度的计时函数。目标的第二部分是对下拉列表下部的不透明度进行动画处理expanded == true。
问题:
扩展视图时动画按预期运行,没有问题。但是当折叠下拉菜单时,似乎只有不透明动画运行。问题在于高度,没有正确反映在视图中。它保持与展开时相同的高度。
这是组件:
const ListItem = (props) => {
const [checkInModal, setCheckInModal] = useState(false);
const [animatedHeight, setAnimatedHeight] = useState(new Animated.Value(0))
const [animatedOpacity] = useState(new Animated.Value(0))
const [expanded, setExpanded] = useState(false);
const toggleDropdown = () => {
console.log("BEFORE ANIMATION =>" + "\n" + "expanded: " + expanded + "\n" + "animatedHeight: " + JSON.stringify(animatedHeight) + "\n" + "animatedOpacity: " + JSON.stringify(animatedOpacity))
if (expanded == true) {
// collapse dropdown
Animated.parallel([
Animated.timing(animatedHeight, {
toValue: 0,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 0,
duration: 400,
})
]).start()
setTimeout(() => console.log("AFTER ANIMATION =>" + "\n" + "animatedHeight: " + JSON.stringify(animatedHeight) + "\n" + "animatedOpacity: " + JSON.stringify(animatedOpacity)), 3000)
// This alone works properly*
// Animated.timing(animatedHeight, {
// toValue: 0,
// duration: 200,
// }).start()
} else {
// expand dropdown
Animated.sequence([
Animated.timing(animatedHeight, {
toValue: 100,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 100,
duration: 400,
})
]).start()
setTimeout(() => console.log("AFTER ANIMATION =>" + "\n" + "animatedHeight: " + JSON.stringify(animatedHeight) + "\n" + "animatedOpacity: " + JSON.stringify(animatedOpacity)), 3000)
// This alone works properly*
// Animated.timing(animatedHeight, {
// toValue: 100,
// duration: 200,
// }).start()
}
setExpanded(!expanded)
}
const interpolatedHeight = animatedHeight.interpolate({
inputRange: [0, 100],
outputRange: [75, 225]
})
const interpolatedOpacity = animatedOpacity.interpolate({
inputRange: [0, 100],
outputRange: [0.0, 1.0]
})
return (
<Animated.View
style={[styles.container, { height: interpolatedHeight }]}
>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', }}>
<View style={styles.leftContainer}>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={styles.title}>{props.title}</Text>
</View>
<Text style={styles.subtitle}>{time()}</Text>
</View>
<View style={styles.rightContainer}>
<TouchableOpacity onPress={() => toggleDropdown()} style={styles.toggleBtn}>
<Image source={require('../assets/img/chevron-down.png')} resizeMode={'contain'} style={styles.chevron} />
</TouchableOpacity>
</View>
</View>
{expanded == true ? (
<Animated.View style={[styles.bottomContainer, { opacity: interpolatedOpacity }]}>
<Components.BodyText text="Subject:" style={{ fontFamily: Fonts.OPENSANS_BOLD }} />
<Components.BodyText text={props.subject} />
</Animated.View>
) : null}
</Animated.View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: '#fff',
borderRadius: 25,
width: width * 0.95,
marginBottom: 5,
marginHorizontal: 5,
paddingVertical: 15,
paddingHorizontal: 15
},
leftContainer: {
justifyContent: 'space-between',
},
rightContainer: {
flexDirection: 'row',
alignItems: 'center'
},
title: {
fontFamily: Fonts.OPENSANS_BOLD,
fontSize: 20,
color: '#454A66'
},
subtitle: {
color: '#454A66',
fontSize: 14
},
typeIcon: {
height: 25,
width: 25
},
chevron: {
height: 15,
width: 15
},
toggleBtn: {
borderWidth: 1,
borderColor: Colors.PRIMARY_DARK,
borderRadius: 7,
paddingTop: 4,
paddingBottom: 2.5,
paddingHorizontal: 4,
marginLeft: 10
},
bottomContainer: {
marginVertical: 20
},
buttonContainer: {
flexDirection: 'row',
width: 250,
justifyContent: 'space-between',
alignSelf: 'center',
marginVertical: 20
},
noShadow: {
elevation: 0,
shadowOffset: {
width: 0,
height: 0
},
shadowRadius: 0,
}
});
export default ListItem;
Run Code Online (Sandbox Code Playgroud)
控制台日志:
折叠时,按按钮展开会在控制台中记录以下内容:
LOG BEFORE ANIMATION =>
expanded: false
animatedHeight: 0
animatedOpacity: 0
LOG AFTER ANIMATION =>
animatedHeight: 100
animatedOpacity: 100
Run Code Online (Sandbox Code Playgroud)
展开后,按按钮折叠会在控制台中记录以下内容:
LOG BEFORE ANIMATION =>
expanded: true
animatedHeight: 100
animatedOpacity: 100
LOG AFTER ANIMATION =>
animatedHeight: 100
animatedOpacity: 100
Run Code Online (Sandbox Code Playgroud)
不太确定那是什么,鉴于如上所述,不透明动画适用于折叠和扩展。如果高度动画根本没有被触及,除了将其从 中取出之外Animated.parallel(),那么高度动画在展开和折叠时将按预期工作。
对这里发生的事情有什么想法吗?
当您尝试对不再渲染的内容进行动画处理时,就会出现问题。Animated.parallel显然,如果其中一个元素不再存在,整个块将无法工作。如果一个动画停止,里面的所有动画Animated.parallel都会停止。您可以使用标志覆盖此行为stopTogether:
const toggleDropdown = () => {
if (expanded === true) {
// collapse dropdown
Animated.parallel([
Animated.timing(animatedHeight, {
toValue: 0,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 0,
duration: 400,
})
], { stopTogether: false }).start()
// ...
Run Code Online (Sandbox Code Playgroud)
当您尝试折叠下拉列表时,expanded标志设置为false,使“主题”组件不再渲染,从而使整个动画失败。展开下拉列表时,expanded标志被设置为true,并且组件现在被渲染,动画可以工作,但这并不重要,因为它之前没有返回到原始值。
尝试删除条件渲染expanded == true ?部分(我认为这不会影响实际输出)。我已经在没有它的情况下尝试过你的代码并且似乎可以工作。
另一种选择是setExpanded(true)在展开下拉列表之前,使元素可见并准备好进行动画处理(并在折叠动画结束时将其设置为 false):
const toggleDropdown = () => {
if (expanded === true) {
// collapse dropdown
Animated.parallel([
Animated.timing(animatedHeight, {
toValue: 0,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 0,
duration: 400,
})
]).start(() => setExpanded(false))
}
else {
setExpanded(true)
// expand dropdown
Animated.sequence([
Animated.timing(animatedHeight, {
toValue: 100,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 100,
duration: 400,
})
]).start()
}
}
Run Code Online (Sandbox Code Playgroud)
您将面临一些视觉问题(“主题”部分显示在组件外部,也许您应该添加overflow: hidden或更改时间)。但动画问题应该得到解决。
| 归档时间: |
|
| 查看次数: |
2885 次 |
| 最近记录: |