5 개의 아이콘으로 구성된 React Native 구성 요소를 연속적으로 만들었습니다. 아이콘을 클릭 할 수 있으며 클릭 한 애니메이션을 적용하려고합니다.React Native : 몇 가지 요소 중 하나만 애니메이션하기
내 문제는 : 아이콘을 클릭하면 모든 아이콘이 움직입니다. 이것은 루프에서 생성되고 모두 동일한 특성을 갖기 때문입니다.
어떻게 눌러도 하나의 아이콘 만 움직일 수 있도록 구성 요소를 설정할 수 있습니까?
는 다음 구성 요소의 :
import React from 'react';
import { StyleSheet, Animated, View, Text, TouchableHighlight, } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
const AnimatedIcon = Animated.createAnimatedComponent(Icon);
export class IconRow extends React.Component {
constructor(props) {
super(props);
this.state = { iconFontSize: new Animated.Value(50) };
}
onIconPress = (index) => {
Animated.sequence([
Animated.timing(this.state.iconFontSize, { toValue: 40, duration: 100 }),
Animated.timing(this.state.iconFontSize, { toValue: 58, duration: 100 }),
Animated.timing(this.state.iconFontSize, { toValue: 50, duration: 100 }),
]).start();
}
renderIcons() {
var icons = [];
for (var i = 0; i < 5; i++) {
icons.push(
<TouchableHighlight key={i} underlayColor="transparent" onPress={this.onIconPress.bind(this, i)}>
<AnimatedIcon name="heart" style={{fontSize:this.state.iconFontSize, color: "red"}} />
</TouchableHighlight>
);
}
return icons;
}
render() {
return (
<View style={{flexDirection: "row"}}>
{this.renderIcons()}
</View>
);
}
}
스낵 : 여기 https://snack.expo.io/HJJ0Edhlz
이것은 작동해야하지만 상태를 변경하는 대신 어레이에 저장하고'setState'를 사용하여 루프 이후의 상태를 업데이트해야합니다. –
@Matt Aft 내가 제안한 것 이상으로 변경 한 사항이 있습니까? – trevor
예, 그냥 연결하지 않아도됩니다. 방금 그 부분을 편집했지만 그 밖의 모든 것들이 잘 어울립니다. –