是否可以为 React Native 中的动画分配一个值(id)?
Is it possible to assign a value (id) to animations in React Native?
如果我的动画被迭代 20 次,但我不希望所有动画同时播放。我将如何触发一个值为 1
的特定动画。如果我点击了 1
那么除了 1
.
其他 19 个应该不会触发
export default class AnimateScreen extends React.PureComponent {
constructor(props){
super(props);
this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
this.state = {
dataSource: '',
progress: new Animated.Value(0),
};
animate = (id) => {
Animated.timing(this.state.progress, {
toValue: 1,
duration: 5000,
easing: Easing.linear,
}).start([id]); <!-- Here is my attempt in trying to animate that one specific animation.
render(){
return(
<FlatList
data={this.state.dataSource}
renderItem={({item}) =>
<View>
<View>
<Text>Work in progress</Text>
<View>
<TouchableHighlight
onPress={this.animate.bind(this, item.id)}>
<Animation
progress={this.state.progress}
source={require('../tools/animations/heart_icon.json')}
/>
</TouchableHighlight>
<Text> Hello</Text>
</View>
</View>
</View>
}
keyExtractor={(item, index) => index.toString()}
/>
);
}
}
我试过了,仍然触发了所有动画。有没有办法专门触发它们?
数据源:
"dataSource":[{"id":"10","images":"Emerson live in the sunshine swim in the sea drink the wild air.jpg","note":"Hello","tag":"sunshine"}
componentDidUpdate(prevProps, prevState) {
if (!prevState.dataSource) {
return fetch(`https://www.website.com/React/json-data.php` , {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
dataSource: responseJson,
},function() {
// In this block you can do something with new state.
});
})
.catch((error) => {
console.error(error);
});
}
}
基本上,您可以将进度键添加到数据源中的每个项目,动画值为 0,然后在单击时您将为该项目的进度设置动画。它应该大致如下所示:
export default class AnimateScreen extends React.PureComponent {
constructor(props){
super(props);
this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
this.state = {
dataSource: [],
progress: new Animated.Value(0),
};
componentDidUpdate(prevProps, prevState) {
if (!prevState.dataSource) {
return fetch(`https://www.website.com/React/json-data.php` , {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
dataSource: responseJson.map(item => ({ ...item, progress: new Animated.Value(0) })), // Add progress key
},function() {
// In this block you can do something with new state.
});
})
.catch((error) => {
console.error(error);
});
}
}
animate = (item) => {
Animated.timing(item.progress, {
toValue: 1,
duration: 5000,
easing: Easing.linear,
}).start(); <!-- Here is my attempt in trying to animate that one specific animation.
render(){
return(
<FlatList
data={this.state.dataSource}
renderItem={({item}) =>
<View>
<View>
<Text>Work in progress</Text>
<View>
<TouchableHighlight
onPress={() => this.animate(item)}>
<Animation
progress={item.progress}
source={require('../tools/animations/heart_icon.json')}
/>
</TouchableHighlight>
<Text> Hello</Text>
</View>
</View>
</View>
}
keyExtractor={(item, index) => index.toString()}
/>
);
}
}
如果我的动画被迭代 20 次,但我不希望所有动画同时播放。我将如何触发一个值为 1
的特定动画。如果我点击了 1
那么除了 1
.
export default class AnimateScreen extends React.PureComponent {
constructor(props){
super(props);
this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
this.state = {
dataSource: '',
progress: new Animated.Value(0),
};
animate = (id) => {
Animated.timing(this.state.progress, {
toValue: 1,
duration: 5000,
easing: Easing.linear,
}).start([id]); <!-- Here is my attempt in trying to animate that one specific animation.
render(){
return(
<FlatList
data={this.state.dataSource}
renderItem={({item}) =>
<View>
<View>
<Text>Work in progress</Text>
<View>
<TouchableHighlight
onPress={this.animate.bind(this, item.id)}>
<Animation
progress={this.state.progress}
source={require('../tools/animations/heart_icon.json')}
/>
</TouchableHighlight>
<Text> Hello</Text>
</View>
</View>
</View>
}
keyExtractor={(item, index) => index.toString()}
/>
);
}
}
我试过了,仍然触发了所有动画。有没有办法专门触发它们?
数据源:
"dataSource":[{"id":"10","images":"Emerson live in the sunshine swim in the sea drink the wild air.jpg","note":"Hello","tag":"sunshine"}
componentDidUpdate(prevProps, prevState) {
if (!prevState.dataSource) {
return fetch(`https://www.website.com/React/json-data.php` , {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
dataSource: responseJson,
},function() {
// In this block you can do something with new state.
});
})
.catch((error) => {
console.error(error);
});
}
}
基本上,您可以将进度键添加到数据源中的每个项目,动画值为 0,然后在单击时您将为该项目的进度设置动画。它应该大致如下所示:
export default class AnimateScreen extends React.PureComponent {
constructor(props){
super(props);
this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
this.state = {
dataSource: [],
progress: new Animated.Value(0),
};
componentDidUpdate(prevProps, prevState) {
if (!prevState.dataSource) {
return fetch(`https://www.website.com/React/json-data.php` , {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
dataSource: responseJson.map(item => ({ ...item, progress: new Animated.Value(0) })), // Add progress key
},function() {
// In this block you can do something with new state.
});
})
.catch((error) => {
console.error(error);
});
}
}
animate = (item) => {
Animated.timing(item.progress, {
toValue: 1,
duration: 5000,
easing: Easing.linear,
}).start(); <!-- Here is my attempt in trying to animate that one specific animation.
render(){
return(
<FlatList
data={this.state.dataSource}
renderItem={({item}) =>
<View>
<View>
<Text>Work in progress</Text>
<View>
<TouchableHighlight
onPress={() => this.animate(item)}>
<Animation
progress={item.progress}
source={require('../tools/animations/heart_icon.json')}
/>
</TouchableHighlight>
<Text> Hello</Text>
</View>
</View>
</View>
}
keyExtractor={(item, index) => index.toString()}
/>
);
}
}