react-native - 移动对象时无法插入颜色
问题描述
import React, { Component } from 'react';
import { View, Animated } from 'react-native';
const ANIMATION_DURATION = 250;
class Ball extends Component {
componentWillMount() {
this.position = new Animated.ValueXY(0,0);
this.borderC = new Animated.Value(0);
Animated.parallel([
Animated.timing(this.position, {
toValue: { x: 200, y: 500 },
duration: ANIMATION_DURATION
}),
Animated.timing(this.borderC, {
toValue: 1,
duration: ANIMATION_DURATION,
})
]).start();
}
render() {
const styl = {
ball: {
height: 60,
width: 60,
borderRadius: 30,
borderWidth: 30,
borderColor: this.borderC.interpolate({
inputRange: [0, 1],
outputRange: ['green', 'yellow'],
})
}
}
return (
<Animated.View style={this.position.getLayout()}>
<View style={styl.ball}/>
</Animated.View>
);
}
}
export default Ball
我有一个简单的组件,它试图将球从一个点移动到另一个点,同时将颜色从绿色变为黄色。没有错误被抛出并且球确实移动。但是我无法弄清楚哪个部分可能出错了。
我已经实现了让动画同时Animated.parallel
运行并interpolate
在borderColor
withinputRange
以及1 and 0
outputRange
这是你玩转的世博小吃
解决方案
您View
需要border-color
转换的第二个组件也应该是一个Animated.View
。
样本
render() {
const style = {
ball: {
height: 60,
width: 60,
borderRadius: 30,
borderWidth: 30,
borderColor: this.borderC.interpolate({
inputRange: [0, 1],
outputRange: ['green', 'yellow'],
})
}
}
return (
<Animated.View style={this.position.getLayout()}>
<Animated.View style={styl.ball}/>
</Animated.View>
);
}
推荐阅读
- android - 使用 Orchestrator 时在 Android 中使用 Jacoco 的代码覆盖率
- javascript - Firebase 查询类别中的产品(数据库设置和最佳实践)
- javascript - socket.io 可以连接到 ws:// 吗?
- java - 我将如何修改 3d 洞穴系统,使其不会从表面去除尽可能多的材料?
- java - 为什么我的构造函数出现非静态变量错误?
- c# - MSAL.net prompt=select_account/forcelogin
- javascript - 我无法使用来自 ajax 的数据属性选择所有元素
- reactjs - 将 axios 发布请求反应到 wcf Web 服务
- python - 无法检索要在其他类中使用的类变量
- java - 在 gradle 构建中执行时显示错误的 Gradle 任务