react-native - 轻敲一个移动的动画对象
问题描述
我想编写一个简单的应用程序,其中对象在屏幕上移动,目标是点击它们。我将动画 API 包含在componentDidMount
:
Animated.timing(
this.state.fadeAnim,
{
toValue: 1,
duration: 10000,
}
).start();
然后我会在我的 React 组件的render
方法中设置样式:
<TouchableHighlight onPress={this.setText} >
<Animated.View style={{
opacity: this.state.fadeAnim, // Binds directly
transform: [{
translateY: this.state.fadeAnim.interpolate({
inputRange: [0, 1],
outputRange: [500, 0] // 0 : 150, 0.5 : 75, 1 : 0
}),
}],
}}>
<Image source={pic} style={{width: 193, height: 110, display: display}}/>
</Animated.View>
</TouchableHighlight>
问题是图像在移动时对点击没有反应。当我点击它的最终目的地时它确实如此,但它在移动时所处的位置。你们能帮我解决这个问题吗?或者 React-Native 不是这类应用程序的最佳工具,我应该转向原生开发吗?
解决方案
首先,我们可以在这里使用原生驱动
Animated.timing(
this.state.fadeAnim,
{
toValue: 1,
duration: 10000,
useNativeDriver: true
}
).start();
并且transform
样式应该被赋予TouchableHighlight
. 问题是Image
正在移动,但按钮不是,因此不会触发 onClick。
<Animated.View style={{
opacity: this.state.fadeAnim, // Binds directly
transform: [{
translateY: this.state.fadeAnim.interpolate({
inputRange: [0, 1],
outputRange: [500, 0] // 0 : 150, 0.5 : 75, 1 : 0
}),
}],
}}>
<TouchableHighlight onPress={this.setText} >
<Image source={pic} style={{width: 193, height: 110, display: display}}/>
</TouchableHighlight>
</Animated.View>
这应该有效。也反应原生应该很好。
推荐阅读
- html - HTML 电子邮件中的粗体内联文本被推送到 Outlook 中的新行
- xml - XPath 用于在 Google 表格中使用 ImportXML 标记
- xcode - 自制软件无法在 Mojave 上识别正确的 xcode 版本
- git - 在 Github 中添加 git 标签时触发发布管道
- android - 如何为 Google 广告请求的 SSL 跟踪配置 Charles 代理?
- c# - C# - Nunit - nunit3-console 没有在 where 过滤器中选择提到的类文件
- asp.net-core - Asp.net Identity Core 中的 AspNetRoleClaims 有什么意义?
- ruby-on-rails - Rails 模板错误隐式转换字符串到字符串 Heroku
- sas - 如何在 SAS 宏变量中引用控制字符(例如 CRLF)
- c++ - c ++从ifstream读取一个块并写入ofstream