react-native - 如何在本机反应中添加对 Animated.sequence 的函数调用
问题描述
我的屏幕中间有一个按钮。onScroll 我希望按钮缩小到 0 以消失,然后缩小到 1 以重新出现在屏幕底部的新位置。我希望能够在缩小和放大动画之间调用 setState (控制按钮的位置)。类似于下面的代码。知道在这两个动画之间添加函数调用的最佳方法吗?或者更好的方式来做到这一点?
animateScale = () => {
return (
Animated.sequence([
Animated.timing(
this.state.scale,
{
toValue: 0,
duration: 300
}
),
this.setState({ positionBottom: true }),
Animated.timing(
this.state.scale,
{
toValue: 1,
duration: 300
}
)
]).start()
)
}
解决方案
经过更多研究,我发现 answer.start() 采用回调函数,如下所示:
这是我的最终解决方案:
export default class MyAnimatedScreen extends PureComponent {
state = {
scale: new Animated.Value(1),
positionUp: true,
animating: false
}
animationStep = (toValue, callback) => () => (
Animated.timing(this.state.scale, {
toValue,
duration: 200
}).start(callback)
)
beginAnimation = (value) => {
if (this.state.animating) return
this.setState({ animating: true }, this.animationStep(0, () => {
this.setState({ positionUp: value, animating: false },
this.animationStep(1))
}))
}
handleScrollAnim = ({ nativeEvent }) => {
const { y } = nativeEvent.contentOffset
if (y < 10) {
if (!this.state.positionUp) {
this.beginAnimation(true)
}
} else if (this.state.positionUp) {
this.beginAnimation(false)
}
}
render() {
return (
<View>
<Animated.View
style={[
styles.buttonWrapper,
{ transform: [{ scale: this.state.scale }] },
this.state.positionUp ? styles.buttonAlignTop :
styles.buttonAlignBottom
]}
>
<ButtonCircle />
</Animated.View>
<ScrollView
onScroll={this.handleScrollAnim}
>
// scroll stuff here
</ScrollView>
</View>
)
}
}
推荐阅读
- git - 如何解决“接收失败:对等方重置连接”错误
- python - 如何将事件从网页发送到客户端应用程序
- react-native - org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 类型的对象的未知属性“supportLibVersion”
- java - 来自 JPA 连接的高效派生计数
- html - 如何在Angular 7中使用带有输入字段的日历图标
- android - Android P - 从 android 版本 9 中的资产复制数据库后出现“SQLite:没有这样的表错误”
- video-streaming - 如何使用 Ant Media Server 进行负载测试?
- apache - 使用 Apache 虚拟主机从 URL 匹配组织
- python - 连接失败弹性搜索python
- c - 为什么我的类型定义结构在另一个标题中不可见?- C