0
사용자가 단추를 탭하여 항목을 "저장"하거나 "저장"을 제거할지 여부를 애니메이션으로 전환하는 저장 단추 (기본적으로 화면을 책갈피에 추가)를 만들려고합니다. 버튼을 가볍게 치는 것은 그것이 "on"애니메이션이든 "off"애니메이션이든 관계없이 애니메이션 중 하나를 호출해야합니다.Lottie를 사용하여 React Native에서 런타임시 애니메이션을 어떻게 토글합니까?
iOS 외에도 React Native의 버튼으로 애니메이션을 전환하는 데 대한 설명서는 없습니다. 어떤 아이디어라도 감사 할 것입니다.
는 다음 구성 요소의 :
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
TouchableOpacity
} from 'react-native';
import _ from 'lodash';
import { connect } from 'react-redux';
import { toggleSaved } from '../../actions';
import Animation from 'lottie-react-native';
import onAnimation from '../animations/heart_On.json';
import offAnimation from '../animations/heart_Off.json';
class SaveButton extends Component {
isSaved =() => {
let item = this.props.item;
if (_.find(this.props.saved, function(i) { return i.type == item.type && i.id == item.id; })) {
return true;
} else {
return false;
}
}
toggleSaved = (saved, item) => {
const { dispatch } = this.props;
dispatch(toggleSaved(saved, item));
this.animation.play();
}
render() {
return (
<TouchableOpacity
onPress={() => this.toggleSaved(this.props.saved, this.props.item) }
>
<Animation
ref={ animation => {
this.animation = animation;
} }
style={ styles.icon }
loop={ false }
source={ this.isSaved() ? onAnimation: offAnimation }
/>
</TouchableOpacity>
);
}
}
SaveButton.propTypes = {
dispatch: PropTypes.func.isRequired,
saved: PropTypes.array.isRequired,
item: PropTypes.object.isRequired
};
const styles = StyleSheet.create({
icon: {
width: 30,
height: 30,
marginTop: 5,
marginRight: 10
}
});
function mapStateToProps(state) {
const { saved } = state.saved;
return {
saved
};
}
export default connect(mapStateToProps)(SaveButton);